Leetcode - 205 Isomorphic Strings (Easy)
Input: s = "egg", t = "add"
Output: true
Input: s = "foo", t = "bar"
Output: false
解题思路:判断上一次字母出现的位置是否相同。
public boolean isIsomorphic(String s, String t) {
int[] preIndexOfS = new int[256];
int[] preIndexOfT = new int[256];
for (int i = 0; i < s.length(); i++) {
char sc = s.charAt(i);
char tc = t.charAt(i);
if (preIndexOfS[sc] != preIndexOfT[tc]) {
return false;
}
preIndexOfS[sc] = i + 1;
preIndexOfT[tc] = i + 1;
}
return true;
}