https://leetcode.com/problems/isomorphic-strings/description/
注意 “ab” to "aa" => false 的情况。
class Solution {
public boolean isIsomorphic(String s, String t) {
Character[] map = new Character[256];
boolean[] visited = new boolean[256]; // "ab" -> "aa" => false
for (int i = 0; i < s.length(); i++) {
char sChar = s.charAt(i);
char tChar = t.charAt(i);
if (map[sChar] == null) {
if (visited[tChar]) {
return false;
}
map[sChar] = tChar;
} else if (map[sChar] != tChar) {
return false;
}
visited[tChar] = true;
}
return true;
}
}
网友评论