Skip to content

Commit cdfdea9

Browse files
Implement isIsomorphic method in IsomorphicStrings class
1 parent 60a2d93 commit cdfdea9

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

Strings/IsomorphicStrings.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class IsomorphicStrings {
2+
public boolean isIsomorphic(String s, String t) {
3+
Map<Character, Character> map1 = new TreeMap<>();
4+
Map<Character, Character> map2 = new TreeMap<>();
5+
6+
if(s.length() != t.length()){
7+
return false;
8+
}
9+
10+
for(int i = 0; i<s.length(); i++){
11+
if(!map1.containsKey(s.charAt(i))){
12+
map1.put(s.charAt(i), t.charAt(i));
13+
}
14+
if(map1.get(s.charAt(i)) != t.charAt(i)){
15+
return false;
16+
}
17+
if(!map2.containsKey(t.charAt(i))){
18+
map2.put(t.charAt(i), s.charAt(i));
19+
}
20+
if(map2.get(t.charAt(i)) != s.charAt(i)){
21+
return false;
22+
}
23+
}
24+
25+
return true;
26+
}
27+
}

0 commit comments

Comments
 (0)