Last updated on October 5th, 2024 at 04:20 pm

Here, We see ** Isomorphic Strings LeetCode Solution**. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

*List of all LeetCode Solution*

*List of all LeetCode Solution*

## Topics

Hash Table, Tree

## Companies

## Level of Question

Medium

**Isomorphic Strings LeetCode Solution**

## Table of Contents

**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

**1. Isomorphic Strings Leetcode Solution 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; } };

**2. Isomorphic Strings Leetcode Solution 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. Isomorphic Strings Leetcode Solution 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; };

**4. Isomorphic Strings Leetcode Solution 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