Leetcode

535 查看

public class Solution {
public boolean isIsomorphic(String s, String t) {
//程序员的世界就是for循环。能一个for循环的就不要两个.那就意味着必须时间换空间

    int n=s.length();
    if(n==1){
        return  true;
    }
    if(s.equals(t)){
        return  true;
    }
  char[] maps=new char[128];
  char[] mapt=new char[128];
for(int i=0;i<n;i++){
    //fboof,engge
    //maps:fe, bn,og 
    //mapst: ef  nb go
    //fboof,engme
    //maps:fe, bn,om   //og被覆盖了
    //mapst: ef  nb go mo  //没有被覆盖掉 ,数量不就不一样了


    /**aba  baa;
     * 
     *maps: aa ba
     * mapt: ba aa
     * key 一定是互换的。这里就不互换了。与顺序有关,那么hashmap就不太适合,用数组,map的作用主要是去除重复。但是没有顺序.
     * 
     */
    /**abb  baa;
     * 
     *maps: ab ba
     * mapt: ba ab
     * 比较上面两个,key和value一定不相等
     * 
     */
    /**
     * 
     * 
     * ab , ac
     * aa  bc 
     * aa  cb 
     * 
     * 
     * key 一定是互换的。其实与顺序没有关系
     */

if ((maps[s.charAt(i)] != 0 && maps[s.charAt(i)] != t.charAt(i)) || (mapt[t.charAt(i)] != 0 && mapt[t.charAt(i)] != s.charAt(i))) {
return false;
}
maps[s.charAt(i)]=t.charAt(i);
mapt[t.charAt(i)]=s.charAt(i);
}
return true;

}

}