同构字符串

给定两个字符串 和 t,判断它们是否是同构的。

如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。

input:s = "paper", 
        t = "title"

output:true
class Solution {
    public boolean isIsomorphic(String s, String t) {
        if(s.length() != t.length()) return false;
        Map<Character,Character> mapS = new HashMap<Character,Character>();
        Map<Character,Character> mapT = new HashMap<Character,Character>();
        for(int i = 0;i < s.length();i ++){
            char x = s.charAt(i),y = t.charAt(i);
            if((mapS.containsKey(x) && mapS.get(x) != y) || (mapT.containsKey(y) && mapT.get(y) != x)){
                return false;
            }
            mapS.put(x,y);
            mapT.put(y,x);
        }
        return true;
    }
}

 

上一篇:测试环境 - 基于K8s搭建Prometheus和Grafana环境


下一篇:6. Map集合体系