[LeetCode] Isomorphic Strings

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
bool isIsomorphic(string s, string t) {
unordered_map<char, char> ch;
unordered_set<char> taken;
for(int i =0; i < s.length(); i++) {
if(ch.count(s[i]) and ch[s[i]] != t[i]) return false;
else if(!ch.count(s[i])) {
if(taken.count(t[i])) return false;
taken.insert(t[i]);
ch[s[i]] = t[i];
}
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/17/PS/LeetCode/isomorphic-strings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.