-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet_205.cpp
51 lines (46 loc) · 1.08 KB
/
leet_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
42
43
44
45
46
47
48
49
50
#include <string>
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
bool isomorphic(const string &a, const string &b);
int main(){
string a = "paper";
string b = "title";
cout << isomorphic(a,b) << '\n';
a = "foo";
b = "bar";
cout << isomorphic(a,b) << '\n';
a = "egg";
b = "add";
cout << isomorphic(a,b) << '\n';
a = "paper";
b = "tetle";
cout << isomorphic(a,b) << '\n';
cout << isomorphic("13","42") << '\n';
cout << isomorphic("13",",!") << '\n';
cout << isomorphic("131",",!,") << '\n';
cout << isomorphic("132",",!,") << '\n';
return 0;
}
// use unordered_map
bool isomorphic(const string &s, const string &t){
int size = s.size();
// if(size != t.size()) return false;
unordered_map<char, char> charMap;
vector<bool> charVisited(128, false);
for(int i=0; i<size; ++i){
auto it = charMap.find(s[i]);
// if char not mapped
if(it == charMap.end()){
if(charVisited[t[i]] == true) return false;
charMap[s[i]] = t[i];
charVisited[t[i]] = true;
} else {
if(it->second != t[i]){
return false;
}
}
}
return true;
}