Isomorphic Strings LeetCode Solution

Last updated on January 12th, 2025 at 03:47 am

Here, we see an Isomorphic Strings LeetCode Solution. This Leetcode problem is solved using different approaches in many programming languages, such as C++, Java, JavaScript, Python, etc.

List of all LeetCode Solution

Topics

Hash Table, Tree

Companies

Google

Level of Question

Medium

Isomorphic Strings LeetCode Solution

Isomorphic Strings LeetCode Solution

1. Problem Statement

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.

Example 1:
Input: s = “egg”, t = “add”
Output: true

Example 2:
Input: s = “foo”, t = “bar”
Output: false

Example 3:
Input: s = “paper”, t = “title”
Output: true

2. Coding Pattern Used in Solution

The coding pattern used in this code is “Hash Mapping”. This pattern involves using hash maps (or arrays as hash maps) to establish a one-to-one mapping between characters of two strings. The goal is to ensure that each character in one string maps uniquely to a character in the other string, and vice versa.

3. Code Implementation in Different Languages

3.1 Isomorphic Strings C++

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

3.2 Isomorphic Strings Java

class Solution {
    public boolean isIsomorphic(String s, String t) {
        int map1[]=new int[200];
        int map2[]=new int[200];
        if(s.length()!=t.length())
            return false;
        for(int i=0;i<s.length();i++)
        {
            if(map1[s.charAt(i)]!=map2[t.charAt(i)])
                return false;
            map1[s.charAt(i)]=i+1;
            map2[t.charAt(i)]=i+1;
        }
        return true;
    }
}

3.3 Isomorphic Strings JavaScript

var isIsomorphic = function(s, t) {
    if(s.length != t.length)
        return false;
    const map1 = [256];
    const map2 = [256];
    for(let idx = 0; idx < s.length; idx++){
        if(map1[s.charAt(idx)] != map2[t.charAt(idx)])
            return false;
        map1[s.charAt(idx)] = idx + 1;
        map2[t.charAt(idx)] = idx + 1;
    }
    return true;
};

3.4 Isomorphic Strings Python

class Solution(object):
    def isIsomorphic(self, s, t):
        map1 = []
        map2 = []
        for idx in s:
            map1.append(s.index(idx))
        for idx in t:
            map2.append(t.index(idx))
        if map1 == map2:
            return True
        return False

4. Time and Space Complexity

Time ComplexitySpace Complexity
C++O(n)O(1)
JavaO(n)O(1)
JavaScriptO(n)O(1)
PythonO(n2)O(n)

Scroll to Top