-
Notifications
You must be signed in to change notification settings - Fork 0
/
205.cpp
41 lines (33 loc) · 1.01 KB
/
205.cpp
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
36
37
38
39
40
41
#include <string>
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution
{
public:
bool isIsomorphic(string s, string t)
{
if (s.length() != t.length())
return false;
unordered_map<char, char> mapS;
unordered_map<char, char> mapT;
for (unsigned short int i = 0; i < s.length(); i++)
{
const unsigned char charOfS = s.at(i);
const unsigned char charOfT = t.at(i);
if ((mapS.find(charOfS) != mapS.end() && mapS[charOfS] != charOfT) || (mapT.find(charOfT) != mapT.end() && mapT[charOfT] != charOfS))
return false;
mapS[charOfS] = charOfT;
mapT[charOfT] = charOfS;
}
return true;
}
};
int main()
{
Solution *sol = new Solution();
cout << sol->isIsomorphic("egg", "add") << endl; // true
cout << sol->isIsomorphic("foo", "bar") << endl; // false
cout << sol->isIsomorphic("paper", "title") << endl; // true
return 0;
}