Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Hash
  • Practice Hash
  • MCQs on Hash
  • Hashing Tutorial
  • Hash Function
  • Index Mapping
  • Collision Resolution
  • Open Addressing
  • Separate Chaining
  • Quadratic probing
  • Double Hashing
  • Load Factor and Rehashing
  • Advantage & Disadvantage
Open In App
Next Article:
C Program to check if strings are rotations of each other or not
Next article icon

C Program to check if two given strings are isomorphic to each other

Last Updated : 19 Aug, 2022
Comments
Improve
Suggest changes
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: 

  1. Initialize two arrays of size 256.
  2. Iterate through characters of the given strings and increment the index equal to the ASCII value of the character at ith position.
  3. 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)



Next Article
C Program to check if strings are rotations of each other or not

D

dmlalwani
Improve
Article Tags :
  • C Programs
  • C++
  • C++ Programs
  • Competitive Programming
  • DSA
  • Hash
  • Mathematical
  • School Programming
  • Strings
  • ASCII
Practice Tags :
  • CPP
  • Hash
  • Mathematical
  • Strings

Similar Reads

  • C Program to check if strings are rotations of each other or not
    Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false) Algorithm: areRotations(str1, str2) 1. Create a temp string and store concatenation of str1 to str1 in temp. temp =
    3 min read
  • C++ Program to check if strings are rotations of each other or not
    Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false) Algorithm: areRotations(str1, str2) 1. Create a temp string and store concatenation of str1 to str1 in temp. temp =
    2 min read
  • C++ Program To Check Whether Two Strings Are Anagram Of Each Other
    Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, "abcd" and "dabc" are an anagram of each other. We strongly recommend that you
    7 min read
  • C++ Program to Check if strings are rotations of each other or not | Set 2
    Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples:  Input : ABACD, CDABA Output : True Input : GEEKS, EKSGE Output : True We have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (lon
    2 min read
  • TCP Client-Server Program to Check if a Given String is Palindrome
    Prerequisites: Socket Programming in C/C++, TCP and UDP server using select, UDP Server-Client implementation in C TCP Client-Server Implementation in C This article describes a Client and Server setup where a Client connects, sends a string to the server and the server shows the original string and
    4 min read
  • C++ Program To Check if Two Matrices are Identical
    The below program checks if two square matrices of size 4*4 are identical or not. For any two matrices to be equal, the number of rows and columns in both the matrix should be equal and the corresponding elements should also be equal.  Recommended: Please solve it on "PRACTICE" first, before moving
    2 min read
  • C++ Program for Check if given string can be formed by two other strings or their permutations
    Given a string str and an array of strings arr[], the task is to check if the given string can be formed by any of the string pair from the array or their permutations. Examples: Input: str = "amazon", arr[] = {"loa", "azo", "ft", "amn", "lka"} Output: Yes The chosen strings are "amn" and "azo" whic
    4 min read
  • C Program to Check Whether Two Matrices Are Equal or Not
    Here, we will see how to check whether two matrices are equal or not using a C Program Input: First Matrix: 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 Second matrix: 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 Output: Matrices are equal.Approach: For any two matrices to be equal, the number of rows
    2 min read
  • C++ Program to check if two Arrays are Equal or not
    Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to check if the two arrays are equal or not. Note: Arrays are said to be equal if and only if both arrays contain the same elements and the frequencies of each element in both arrays are the same. Examples: Input: arr1[]
    4 min read
  • C Program To Check If Two Linked Lists Are Identical
    Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences