auto-generated entry for Isomorphic Strings. the solution files are available below.
solution files
- C++
isomorphic-strings/synced-solution.cpp - Python
isomorphic-strings/synced-solution.py - TypeScript
isomorphic-strings/synced-solution.ts
Collected C++, Python, TypeScript solutions for isomorphic strings. Add a dedicated write-up later if you want deeper notes.
auto-generated entry for Isomorphic Strings. the solution files are available below.
isomorphic-strings/synced-solution.cppisomorphic-strings/synced-solution.pyisomorphic-strings/synced-solution.tsdef isIsomorphic(s: str, t: str) -> bool:
if len(s) != len(t):
return False
s_to_t = {}
t_to_s = {}
for char_s, char_t in zip(s, t):
if char_s in s_to_t:
if s_to_t[char_s] != char_t:
return False
else:
s_to_t[char_s] = char_t
if char_t in t_to_s:
if t_to_s[char_t] != char_s:
return False
else:
t_to_s[char_t] = char_s
return True
class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.length() != t.length()) return false;
unordered_map<char, char> sToT;
unordered_map<char, char> tToS;
for (int i = class="syntax-number">0; i < s.length(); i++) {
char charS = s[i];
char charT = t[i];
if (sToT.count(charS)) {
if (sToT[charS] != charT) return false;
} else {
sToT[charS] = charT;
}
if (tToS.count(charT)) {
if (tToS[charT] != charS) return false;
} else {
tToS[charT] = charS;
}
}
return true;
}
};
function isIsomorphic(s: string, t: string): boolean {
if (s.length !== t.length) return false;
const sToT = new Map<string, string>();
const tToS = new Map<string, string>();
for (let i = class="syntax-number">0; i < s.length; i++) {
const charS = s[i];
const charT = t[i];
if (sToT.has(charS)) {
if (sToT.get(charS) !== charT) return false;
} else {
sToT.set(charS, charT);
}
if (tToS.has(charT)) {
if (tToS.get(charT) !== charS) return false;
} else {
tToS.set(charT, charS);
}
}
return true;
}