Posts Isomorphic Strings
Post
Cancel

Isomorphic Strings

PROBLEM DESCRIPTION

Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

leetcode

SOLUTION

NeetCode YouTube

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
class Solution {
    
    public boolean isIsomorphic(String s, String t) {
        
        // Two HashMaps to store mappings between characters of s and t in both directions.
        // m1 will store the mapping from s to t, and m2 will store the mapping from t to s.
        // IMPORTANT: we need to maintain both sides because as per the question, two character cannot have mapping
        Map<Character, Character> m1 = new HashMap<>();
        Map<Character, Character> m2 = new HashMap<>();

        // Traverse each character in the strings s and t.
        for (int i = 0; i < s.length(); i++) {

            // Get the current characters from both strings.
            Character c1 = s.charAt(i);
            Character c2 = t.charAt(i);

            // Check if there's a mismatch in the mappings between s and t.
            // If there's a mismatch, it means that the characters cannot be isomorphic,
            // so we return false.
            if ((m1.containsKey(c1) && m1.get(c1) != c2) 
                    || (m2.containsKey(c2) && m2.get(c2) != c1)) {
                return false;
            }

            // Update the mappings in both m1 and m2.
            // For the character c1 in s, we map it to character c2 in t in m1.
            // For the character c2 in t, we map it to character c1 in s in m2.
            m1.put(c1, c2);
            m2.put(c2, c1);
        }

        return true;
    }
}
This post is licensed under CC BY 4.0 by the author.