forked from super30admin/Hashing-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsomorphicStrings.java
35 lines (33 loc) · 1.08 KB
/
IsomorphicStrings.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
31
32
33
34
35
// Time Complexity :O(n)
// Space Complexity :O(1)
// Did this code successfully run on Leetcode :Yes
// Any problem you faced while coding this :
class Solution {
public boolean isIsomorphic(String s, String t) {
int sLength = s.length();
int tLength = t.length();
if (sLength != tLength)
return false;
HashMap<Character, Character> sMap = new HashMap<Character, Character>();
HashMap<Character, Character> tMap = new HashMap<Character, Character>();
for (int i = 0; i < sLength; i++) {
char sChar = s.charAt(i);
char tChar = t.charAt(i);
if (!sMap.containsKey(sChar)) {
sMap.put(sChar, tChar);
} else {
if (sMap.get(sChar) != tChar) {
return false;
}
}
if (!tMap.containsKey(tChar)) {
tMap.put(tChar, sChar);
} else {
if (tMap.get(tChar) != sChar) {
return false;
}
}
}
return true;
}
}