Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Isomorphic Strings problem solution

YASH PAL, 31 July 2024

In this Leetcode Isomorphic Strings problem solution we have Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

Leetcode Isomorphic Strings problem solution

Problem solution in Python.

class Solution:
    def isIsomorphic(self, s: str, t: str) -> bool:
        X=[]
        Y=[]
        for i in s:
            X.append(s.index(i))
        for i in t:
            Y.append(t.index(i))
        if X==Y:
            return True
        return False

Problem solution in Java.

class Solution {
    public boolean isIsomorphic(String s, String t) {

         if (s.length() != t.length()) {
            return false;
        }

     return (checkIsoMorphism(s,t) && checkIsoMorphism(t,s));

    }

    private static boolean checkIsoMorphism(String s,String t)
    {
        HashMap<Character, Character> charMap = new HashMap<>();
        boolean result = true;
        for (int i = 0; i < s.length(); i++) {
            if (!charMap.containsKey(s.charAt(i)))
                charMap.put(s.charAt(i), t.charAt(i));
            else {
                if (!((charMap.get(s.charAt(i))) == t.charAt(i))) {
                    
                    result = false;
                    break;

                }

            }
        }

        return(result);
    }
}

Problem solution in C++.

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        return (isIso(s,t) && isIso(t,s));
    }
    bool isIso(string &s, string &t) {
        unordered_map<char, char> m;
        int l = s.length();
        for(int i=0;i<l;i++){
            if(m.count(s[i])) {
                if(m[s[i]] != t[i]) {
                    return false;
                }
            } else {
                m[s[i]]=t[i];
            }
        }
        return true;
    }
};

Problem solution in C.

bool isIsomorphic(char* s, char* t) {
    int s2t[256] = {0};
    int t2s[256] = {0};
    for(; *s != ''; s++,t++)
    {
        if(s2t[*s] == 0)
        {
            if(t2s[*t] != 0)
            {
                return false;
            }
            s2t[*s] = *t;
            t2s[*t] = *s;
        }
        else if(s2t[*s] != *t){
            return false;
        }
    
    }
    return true;
 }

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes