Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptCheck two given strings are isomorphic in JavaScript

Check two given strings are isomorphic in JavaScript

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. We can also explain this by saying that each character of the first string is replaced by each 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.

We can check two given strings are isomorphic in Javascript in two ways:

  • Using Naive Approach
  • Using Hashmap

Approach 1: Using Naive Approach

In this approach, we will compare each character of the first string with another character of the first string likewise for the second string. The current character of both strings shouldn’t be equal to other characters. The time complexity of this approach is O(N^2) where n is the length of the string. This is a brute-force approach which is not very efficient.

This is the implementation of the above approach.

Javascript




function isStringIsomorphic(str1, str2) {
    if (str1.length !== str2.length) {
        return false;
    }
    for (let i = 0; i < str1.length; i++) {
        for (let j = i + 1; j < str1.length; j++) {
            if (
                (str1[i] === str1[j] &&
                str2[i] !== str2[j]) ||
                (str1[i] !== str1[j] &&
                str2[i] === str2[j])
            ) {
                return false;
            }
        }
    }
    return true;
}
 
str1 = "ABCA";
str2 = "XYZX";
console.log(isStringIsomorphic(str1,str2));


Output

true

Approach 2: By using hashmap

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




// 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";
console.log(isIsomorphic(str1, str2));


Output

true

RELATED ARTICLES

Most Popular

Recent Comments