Skip to content

Commit 3ae99c4

Browse files
solvs isomorphic strings
1 parent ae04114 commit 3ae99c4

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: src/IsomorphicStrings.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.HashMap;
2+
import java.util.HashSet;
3+
import java.util.Map;
4+
import java.util.Set;
5+
6+
public class IsomorphicStrings {
7+
public static boolean isIsomorphic(String s, String t) {
8+
Set<Character> mappingsTo = new HashSet<>();
9+
Map<Character, Character> mappings = new HashMap<>();
10+
11+
for (int index = 0 ; index < s.length() ; index++) {
12+
char a = s.charAt(index);
13+
char b = t.charAt(index);
14+
if (mappings.getOrDefault(a, b) != b || (!mappings.containsKey(a) && mappingsTo.contains(b))) {
15+
return false;
16+
}
17+
mappings.put(a, b);
18+
mappingsTo.add(b);
19+
}
20+
21+
return true;
22+
}
23+
}

0 commit comments

Comments
 (0)