forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0205-isomorphic-strings.java
29 lines (25 loc) · 961 Bytes
/
0205-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
classSolution {
publicbooleanisIsomorphic(Strings, Stringt) {
// Needs bidirectional mapping from s <--> t
if(s.length() != t.length()) {
returnfalse;
}
Map<Character, Character> mapSourceToDest = newHashMap<>();
Map<Character, Character> mapDestToSource = newHashMap<>();
intlen = s.length();
for(inti=0; i<len; i++) {
charsourceChar = s.charAt(i), destChar = t.charAt(i);
charsourceReturn = mapSourceToDest.getOrDefault(sourceChar, destChar);
if(sourceReturn != destChar) {
returnfalse;
}
mapSourceToDest.put(sourceChar, destChar);
chardestReturn = mapDestToSource.getOrDefault(destChar, sourceChar);
if(destReturn != sourceChar) {
returnfalse;
}
mapDestToSource.put(destChar, sourceChar);
}
returntrue;
}
}