-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path205.java
More file actions
34 lines (33 loc) · 1.21 KB
/
205.java
File metadata and controls
34 lines (33 loc) · 1.21 KB
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
import java.util.HashMap;
/**
* 205. Isomorphic Strings
*
* Given two strings s and t, determine if they are isomorphic.
* Two strings s and t are isomorphic if the characters in s can be replaced to get t.
* All occurrences of a character must be replaced with another character
* while preserving the order of characters. No two characters may map to the same character,
* but a character may map to itself.
*/
class Solution {
public boolean isIsomorphic(String s, String t) {
var sMap = new HashMap<Character, Character>();
var tMap = new HashMap<Character, Character>();
for (int i = 0; i < s.length(); i++) {
Character sChar = s.charAt(i);
Character tChar = t.charAt(i);
Character sLookup = sMap.getOrDefault(sChar, null);
Character tLookup = tMap.getOrDefault(tChar, null);
if (sLookup != null) {
if (!sLookup.equals(tChar)) return false;
} else {
sMap.put(sChar, tChar);
}
if (tLookup != null) {
if (!tLookup.equals(sChar)) return false;
} else {
tMap.put(tChar, sChar);
}
}
return true;
}
}