LeetCode - Algorithms - 205. Isomorphic Strings

Problem

205. Isomorphic Strings

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public boolean isIsomorphic(String s, String t) {
boolean r = true;
Map<Character,Character> map = new HashMap<Character, Character>();
Map<Character,Character> map2 = new HashMap<Character, Character>();
for(int i=0; i<s.length(); i++) {
char k = s.charAt(i);
char x = t.charAt(i);
if (map.containsKey(k)) {
char v = map.get(k);
if (x!=v) {
return false;
}
}
else {
map.put(k,x);
}
if (map2.containsKey(x)) {
char v = map2.get(x);
if (k!=v) {
return false;
}
}
else {
map2.put(x, k);
}
}
return r;
}
}

Submission Detail

  • 32 / 32 test cases passed.
  • Runtime: 12 ms, faster than 26.96% of Java online submissions for Isomorphic Strings.
  • Memory Usage: 39.6 MB, less than 6.14% of Java online submissions for Isomorphic Strings.