C Program to check if two given strings are isomorphic to each other Last Updated : 19 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 isomorphic if there is a one to one mapping possible for every character of str1 to every character of str2 and all occurrences of every character in str1 map to same character in str2. 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 = "add" 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 ith position.If there 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 isomorphic 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 }; // Traversing 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; } Output: Yes Time Complexity: O(N) Auxiliary Space: O(256) Comment More infoAdvertise with us Next Article Check if two strings are same or not without using library functions D dmlalwani Follow Improve Article Tags : C Language ASCII Similar Reads Check if two strings are same or not Given two strings, the task is to check if these two strings are identical(same) or not. Consider case sensitivity.Examples:Input: s1 = "abc", s2 = "abc" Output: Yes Input: s1 = "", s2 = "" Output: Yes Input: s1 = "GeeksforGeeks", s2 = "Geeks" Output: No Approach - By Using (==) in C++/Python/C#, eq 7 min read Check if two strings are same or not without using library functions Given two strings S1 and S2, the task is to check whether they are the same or not without using string library functions. Examples: Input: S1 = âGeeksForGeeksâ, S2 = âGeeksForGeeksâOutput: TrueExplanation:S1 and S2 are the same strings Input: S1 = âGeeksForGeeksâ, S2 = âGeeksforGeeksâOutput: False 5 min read C program to detect tokens in a C program As it is known that Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens. A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.For Example: 1) Keyword 5 min read How to Compare Characters in C++? char in c is a keyword used for representing character data type. The memory size of char is 1 byte containing numbers, alphabets, and alphanumeric characters. We can compare the characters in C using 2 different ways: Comparison using ASCII values.Using the built-in function.1. Using ASCII Values A 3 min read Output of C Program | Set 17 Predict the output of following C programs. Question 1 C #include<stdio.h> #define R 10 #define C 20 int main() { int (*p)[R][C]; printf("%d", sizeof(*p)); getchar(); return 0; } Output: 10*20*sizeof(int) which is "800" for compilers with integer size as 4 bytes. The pointer p is de- 2 min read Differentiate printable and control character in C ? Given a character we need to find if it printable or not. We also need to find if it is control character or not. A character is known as printable character if it occupies printing space. For the standard ASCII character set (used by the "C" locale), control characters are those between ASCII codes 2 min read Like