forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path205-Isomorphic-Strings.java
More file actions
29 lines (25 loc) · 961 Bytes
/
205-Isomorphic-Strings.java
File metadata and controls
29 lines (25 loc) · 961 Bytes
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
class Solution {
public boolean isIsomorphic(String s, String t) {
// Needs bidirectional mapping from s <--> t
if(s.length() != t.length()) {
return false;
}
Map<Character, Character> mapSourceToDest = new HashMap<>();
Map<Character, Character> mapDestToSource = new HashMap<>();
int len = s.length();
for(int i=0; i<len; i++) {
char sourceChar = s.charAt(i), destChar = t.charAt(i);
char sourceReturn = mapSourceToDest.getOrDefault(sourceChar, destChar);
if(sourceReturn != destChar) {
return false;
}
mapSourceToDest.put(sourceChar, destChar);
char destReturn = mapDestToSource.getOrDefault(destChar, sourceChar);
if(destReturn != sourceChar) {
return false;
}
mapDestToSource.put(destChar, sourceChar);
}
return true;
}
}