-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path205Isomorphic_Strings.java
More file actions
28 lines (23 loc) · 945 Bytes
/
205Isomorphic_Strings.java
File metadata and controls
28 lines (23 loc) · 945 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
import java.util.*; //for hash table
class Solution {
public boolean isIsomorphic(String s, String t) {
Hashtable<Integer, Integer> ht = new Hashtable<Integer, Integer>();
for (int i = 0; i<s.length(); i++){
//System.out.println(ht.containsKey((int)s.charAt(i)));
if(ht.containsKey((int)s.charAt(i))){
//System.out.println(s.charAt(i));
if(ht.get((int)s.charAt(i)) != (int)t.charAt(i)){
//System.out.println(ht.get(s.charAt(i)));
return false;
}
}else{
if(ht.containsValue( (int)t.charAt(i))){
return false; //No two characters may map to the same character
}
}
ht.put((int)s.charAt(i), (int)t.charAt(i));
//System.out.println(ht);
}
return true;
}
}