Given two strings **str1** and **str2**, the task is to check if the two given strings are isomorphic to each other or not.

Two strings are said to be

isomorphicif there is aone to one mappingpossible for every character ofstr1to every character ofstr2and all occurrences of every character instr1map to same character instr2.

**Examples:**

Input:str1 = “egg”, str2 = “add”

Output:Yes

Explanation:

‘e’ in str1 with ASCII value 101 is mapped to ‘a’ in str2 with ASCII value 97.

‘g’ in str1 with ASCII value 103 is mapped to ‘d’ in str2 with ASCII value 100.

Input:str1 = “eggs”, str2 = “addd”

Output:No

**Hashing Approach:** Refer to the previous post for the Hashmap based approach.

**Time Complexity:** O(N)

**Auxiliary Space:** O(256)

**ASCII-value based Approach:** The idea is similar to that of the above approach. Follow the steps below to solve the problem:

- Initialize two arrays of size 256.
- Iterate through characters of the given strings and increment the index equal to the ASCII value of the character at
**i**position.^{th} - If the are no conflicts in the mapping of the characters, print
**Yes**. Otherwise, print**No**.

Below is the implementation of the above approach:

## C

`// C Program to implement ` `// the above approach ` ` ` `#include <stdio.h> ` `#include <string.h> ` `#include <stdbool.h> ` ` ` `// Function to check and return if strings ` `// str1 and str2 are ismorphic ` `bool` `areIsomorphic(` `char` `*str1, ` `char` `*str2) ` `{ ` ` ` `// If the length of the strings ` ` ` `// are not equal ` ` ` `if` `(` `strlen` `(str1) != ` `strlen` `(str2)) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Initialise two arrays ` ` ` `int` `arr1[256] = { 0 }, arr2[256] = { 0 }; ` ` ` ` ` `// Travsersing both the strings ` ` ` `for` `(` `int` `i = 0; i < ` `strlen` `(str1); i++) { ` ` ` ` ` `// If current characters don't map ` ` ` `if` `(arr1[(` `int` `)str1[i]] ` ` ` `!= arr2[(` `int` `)str2[i]]) { ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Increment the count of characters ` ` ` `// at their respective ASCII indices ` ` ` `arr1[(` `int` `)str1[i]]++; ` ` ` `arr2[(` `int` `)str2[i]]++; ` ` ` `} ` ` ` `return` `true` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `char` `s1[] = ` `"aab"` `, s2[] = ` `"xxy"` `; ` ` ` ` ` `if` `(areIsomorphic(s1, s2)) ` ` ` `printf` `(` `"Yes\n"` `); ` ` ` `else` ` ` `printf` `(` `"No\n"` `); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

**Time Complexity:** O(N)

**Auxiliary Space:** O(256)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if two given strings are isomorphic to each other
- Minimize count of given operations required to make two given strings permutations of each other
- Check if two strings are permutation of each other
- Check whether two strings are anagrams of each other using unordered_map in C++
- Check if two strings can be made equal by swapping one character among each other
- Check whether two strings are anagram of each other
- A Program to check if strings are rotations of each other or not
- Check if strings are rotations of each other or not | Set 2
- Check if two given circles touch or intersect each other
- Check if given string can be formed by two other strings or their permutations
- Check if two arrays are permutations of each other using Mathematical Operation
- Check if two Linked Lists are permutations of each other
- Check if two Integer are anagrams of each other
- Check if two arrays are permutations of each other
- Check if a given array contains duplicate elements within k distance from each other
- Given two numbers as strings, find if one is a power of other
- Replace two substrings (of a string) with each other
- Check if all rows of a matrix are circular rotations of each other
- Check if roots of a Quadratic Equation are reciprocal of each other or not
- Check if all the pairs of an array are coprime with each other

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.