-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathisomorphicString.java
More file actions
51 lines (49 loc) · 1.5 KB
/
Copy pathisomorphicString.java
File metadata and controls
51 lines (49 loc) · 1.5 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Character, Character> match = new HashMap<Character, Character>();
for(int i = 0; i < s.length(); i++){
if(match.get(s.charAt(i)) != null){
if(match.get(s.charAt(i)) != t.charAt(i)) return false;
}else{
if(notUse(match, t.charAt(i))){ //see if t's char is already used
match.put(s.charAt(i), t.charAt(i));
}else{
return false;
}
}
}
return true;
}
private boolean notUse(Map<Character, Character> map, char target){
if(map.containsValue(target)){
return false;
}else{
return true;
}
}
}
/* second solution */
public class Solution {
public boolean isIsomorphic(String s, String t) {
Map<Object, Object> hm = new HashMap<>();
for(int i = 0; i < s.length(); i++){
if(!Objects.equals(hm.put(s.charAt(i), i), hm.put(t.charAt(i) + "", i))){
return false;
}
}
return true;
}
}
/* third solution */
public boolean isIsomorphic(String s, String t) {
int[] cache = new int[256];
int[] cache1 = new int[256];
for(int i = 0; i < s.length(); i++){
if(cache[s.charAt(i)] != cache1[t.charAt(i)]){
return false;
}
cache[s.charAt(i)] = i + 1;
cache1[t.charAt(i)] = i + 1;
}
return true;
}