Check if two given strings are isomorphic to each other in C++

Check if strings are isomorphic to Each Other

Today in this article we will be discussing how to check if strings are isomorphic to each other or not.

Lets understand this with the help of example :- 

  • Input:- String 1:- A B A C B A , String 2 :- X P X Z P W
  • Output:-  Not Isomorphic

Two strings str1 and str2 are called isomorphic if there is a one to one mapping possible for every character of str1 to every character of str2 while preserving the order.

Check if strings are isomorphic to Each Other

Algorithm

  • First we will initialize  two character array m1,m2  of 256 that is the maximum character m1 and m2 for two strings s1 and s2. And initialize m1 and m2 to 0 
  • If the length of both the string is not equal then return false.
  • We will loop through the entire length of any one of the string and will check if the character is the first occurrence then will map the characters.
  • otherwise the character is already mapped then will check if it not matched with the last mapping then will return false .
  • Out of the loop we will return true.
  • In the main function that is driver code we will call the function and print if the string is isomorphic or not.
Check if strings are isomorphic

C++ Code :-

Run
#include <iostream>
using namespace std;

// Function that will return true if the string is isomorphic to each other
bool Ismorphic(string s1, string s2) {
    int n = s1.length();
    int ma = s2.length();
    // If the length of the two string is not equal then will return false
    if (n != ma) return false;
    int m1[256] = {0};
    int m2[256] = {0};
    for (int i = 0; i < n; i++) {
        // if the character is found for the first time
        if (!m1[s1[i]] and !m2[s2[1]]) {
            m1[s1[i]] = s2[i];
            m2[s2[i]] = s1[i];
        }
        // if the character is already mapped before
        else if (m1[s1[i]] != s2[i])
            return false;
    }
    return true;
}
int main() {
    // Check if strings are isomorphic to Each Other
    string s1 = "aba", s2 = "xpx";

    if (Ismorphic(s1, s2) == true)
        cout << "Yes strings are isomorphic to Each Other";
    else
        cout << "No strings are not isomorphic to Each Other";
    return 0;
}#include 
using namespace std;

// Function that will return true if the string is isomorphic to each other
bool Ismorphic(string s1, string s2) {
    int n = s1.length();
    int ma = s2.length();
    // If the length of the two string is not equal then will return false
    if (n != ma) return false;
    int m1[256] = {0};
    int m2[256] = {0};
    for (int i = 0; i < n; i++) {
        // if the character is found for the first time
        if (!m1[s1[i]] and !m2[s2[1]]) {
            m1[s1[i]] = s2[i];
            m2[s2[i]] = s1[i];
        }
        // if the character is already mapped before
        else if (m1[s1[i]] != s2[i])
            return false;
    }
    return true;
}
int main() {
    // Check if strings are isomorphic to Each Other
    string s1 = "aba", s2 = "xpx";

    if (Ismorphic(s1, s2) == true)
        cout << "Yes strings are isomorphic to Each Other";
    else
        cout << "No strings are not isomorphic to Each Other";
    return 0;
}

Output:-

 Yes strings are isomorphic to Each Other