Isomorphic Strings LeetCode Solution

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

Isomorphic Strings LeetCode Solution

Isomorphic Strings LeetCode Solution

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

Isomorphic Strings Leetcode Solution C++

class Solution {
    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;
        return true;
};Code language: PHP (php)

Isomorphic Strings Leetcode Solution Java

class Solution {
    public boolean isIsomorphic(String s, String t) {
        int map1[]=new int[200];
        int map2[]=new int[200];
            return false;
        for(int i=0;i<s.length();i++)
                return false;
        return true;
}Code language: JavaScript (javascript)

Isomorphic Strings 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;
};Code language: JavaScript (javascript)

Isomorphic Strings Solution Python

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