8000 Hash:1 · rchen102/Leetcode-Notes@5e496b1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e496b1

Browse files
author
rchen102
committed
Hash:1
1 parent 6cd2a1f commit 5e496b1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Java/leetcode 205.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
// Solution1: HashMap
2+
// T: O(n) S: O(1) (only 256 character)
3+
class Solution {
4+
public boolean isIsomorphic(String s, String t) {
5+
if (s == null || s == null) return false;
6+
if (s.length() != t.length()) return false;
7+
Map<Character, Character> map = new HashMap<>();
8+
Map<Character, Character> revMap = new HashMap<>();
9+
char[] sch = s.toCharArray();
10+
char[] tch = t.toCharArray();
11+
for (int i = 0; i < sch.length; i++) {
12+
if (map.containsKey(sch[i])) {
13+
if (map.get(sch[i]) != tch[i]) return false;
14+
} else {
15+
if (revMap.containsKey(tch[i])) return false;
16+
map.put(sch[i], tch[i]);
17+
revMap.put(tch[i], sch[i]);
18+
}
19+
}
20+
return true;
21+
}
22+
}
23+
124
//Own Solution: HashMap T: O(n) S: O(512)
225
class Solution {
326
public boolean isIsomorphic(String s, String t) {

0 commit comments

Comments
 (0)
0