AlgoMaster Logo

Isomorphic Strings

s=egg,t=add
1public boolean isIsomorphic(String s, String t) {
2    if (s.length() != t.length()) return false;
3
4    int[] mapS = new int[256];
5    int[] mapT = new int[256];
6
7    for (int i = 0; i < s.length(); i++) {
8        char charS = s.charAt(i);
9        char charT = t.charAt(i);
10
11        // Check if previous mapped characters are same
12        if (mapS[charS] != mapT[charT]) return false;
13
14        // Map characters to index+1 to avoid default 0 confusion
15        mapS[charS] = i + 1;
16        mapT[charT] = i + 1;
17    }
18
19    return true;
20}
0 / 15
String s:eggString t:add