-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisomorphicstrings.cpp
More file actions
35 lines (33 loc) · 860 Bytes
/
isomorphicstrings.cpp
File metadata and controls
35 lines (33 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.length()!=t.length())
return false;
unordered_map<char, char> s_2_t;
unordered_map<char, char> t_2_s;
for (size_t i=0; i<s.length(); i++)
{
char s_char = s[i];
char t_char = t[i];
if (s_2_t.find(s_char) != s_2_t.end())
{
if (s_2_t[s_char]!=t_char)
return false;
}
else
{
s_2_t[s_char] = t_char;
}
if (t_2_s.find(t_char) != t_2_s.end())
{
if (t_2_s[t_char]!=s_char)
return false;
}
else
{
t_2_s[t_char] = s_char;
}
}
return true;
}
};