Algorithm
Problem Name: 205. Isomorphic Strings
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
Constraints:
1 <= s.length <= 5 * 104
t.length == s.length
s
andt
consist of any valid ascii character.
Code Examples
#1 Code Example with C Programming
Code -
C Programming
bool isIsomorphic(char* s, char* t) {
char a[128] = { 0 };
char b[128] = { 0 };
while (*s) {
//printf("%c:%c\n", *s, *t);
if (a[*s] == 0 && b[*t] == 0) {
a[*s] = *t;
b[*t] = *s;
} else if (a[*s] == *t && b[*t] == *s) {
} else {
return false;
}
s ++;
t ++;
}
return true;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_mapms, mt;
for (int i = 0; i < s.size(); ++i) {
if (ms.count(s[i]) && mt.count(t[i])) {
if (ms[s[i]] != t[i]) {
return false;
}
} else if (ms.count(s[i]) || mt.count(t[i])) {
return false;
} else {
ms[s[i]] = t[i];
mt[t[i]] = s[i];
}
}
return true;
}
};
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Java Programming
Code -
Java Programming
class Solution {
public boolean isIsomorphic(String s, String t) {
int[] position = new int[512];
for (int i = 0; i < s.length(); i++) {
if (position[s.charAt(i)] != position[t.charAt(i) + 256]) {
return false;
}
position[s.charAt(i)] = position[t.charAt(i) + 256] = i + 1;
}
return true;
}
}
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const isIsomorphic = function(s, t) {
if (s.length !== t.length) {
return false;
}
const smap = {};
const tmap = {};
for (let i = 0; i < s.length; i++) {
if (!smap.hasOwnProperty(s[i])) {
smap[s[i]] = t[i];
}
if (!tmap.hasOwnProperty(t[i])) {
tmap[t[i]] = s[i];
}
if (smap[s[i]] !== t[i] || tmap[t[i]] !== s[i]) return false;
}
return true;
};
Copy The Code &
Try With Live Editor
Input
Output
#5 Code Example with Python Programming
Code -
Python Programming
class Solution:
def isIsomorphic(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
if len(s) != len(t): return False
dic={}
for i in range(len(s)):
if not t[i] in dic.values() and not s[i] in dic: dic[s[i]] = t[i]
elif not s[i] in dic or dic[s[i]] != t[i]: return False
return True
Copy The Code &
Try With Live Editor
Input
Output
#6 Code Example with C# Programming
Code -
C# Programming
using System.Collections.Generic;
namespace LeetCode
{
public class _0205_IsomorphicStrings
{
public bool IsIsomorphic(string s, string t)
{
if (s.Length != t.Length) return false;
var map = new Dictionary < char, char>(26);
var visited = new HashSet(26);
for (int i = 0; i < s.Length; i++)
{
if (map.ContainsKey(s[i]))
{
if (map[s[i]] != t[i]) return false;
}
else
{
if (visited.Contains(t[i])) return false;
map.Add(s[i], t[i]);
visited.Add(t[i]);
}
}
return true;
}
}
}
Copy The Code &
Try With Live Editor
Input
Output