Skip to content
Related Articles

Related Articles

Check two given strings are isomorphic in JavaScript

Improve Article
Save Article
Like Article
  • Last Updated : 30 Dec, 2021

Two strings are said to be isomorphic if it is possible to map every character of the first string to every character of the second string. Basically, in isomorphic strings, there is a one-to-one mapping between every character of the first string to every character of the second string.

Example 1:

str1 = 'ABCA'
str2 = 'XYZX'
'A' maps to 'X'
'B' maps to 'Y'
'C' maps to 'Z'

Here, mapping is possible between every character of the first string to every character of the second string. So str1 and str2 are isomorphic.

Example 2:

str1 = 'ABCA'
str2 = 'WXYZ'
'A' maps to 'W'
'B' maps to 'X'
'C' maps to 'Y'
'A' again maps to 'Z'

These two strings are not isomorphic because character ‘A’ from the first string is mapping with two characters from the second string.

One possible way to check isomorphic strings is if we can replace each character of the first string with a character to get the second string and vice versa.

Approach: To check if strings are isomorphic or not, we have to take care of the following conditions:

  • The length of both strings should be equal
  • The current character of both strings shouldn’t be mapped with other characters already.

We will use a hashmap to store the mapping between characters from str1 to those of str2. We will also use a Set to store the already mapped characters of str2.

Below is the implementation of the above approach.

Javascript




<script>
    // JavaScript program for above approach
 
    // Function to check isomorphic strings
    function isIsomorphic(str1, str2) {
 
        // If length of strings are not equal then
        // they are not isomorphic
        if (str1.length !== str2.length) {
            return false;
        }
 
        // Map to store the mapping between
        // characters of first string to second
        const map = new Map();
 
        // Set to store the already mapped
        // character of second string
        const set = new Set();
 
        for (let i = 0; i < str1.length; i++) {
 
            // Taking ith char from both strings
            char1 = str1.charAt(i);
            char2 = str2.charAt(i);
 
            // If char1 has already been mapped
            if (map.has(char1) == true) {
 
                // Then we have to check that
                // mapped char should be same
                if (map.get(char1) !== char2) {
                    return false;
                }
            }
 
            // If char1 is appearing for the first time
            else {
 
                // Check in the set that the char2
                // is already there or not
                if (set.has(char2)) {
                    return false;
                }
 
                // If none of above condition is true
                // it means both char1 and char2 are
                // appearing for the first time
                // insert them into the map
                map.set(char1, char2);
                set.add(char2);
            }
        }
        return true;
    }
    str1 = "ABCA";
    str2 = "XYZX";
    document.write(isIsomorphic(str1, str2));
</script>


Output:

true

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!