forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
52 lines (40 loc) · 1.16 KB
/
main.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
51
52
/// Source : https://leetcode.com/problems/isomorphic-strings/description/
/// Author : liuyubobobo
/// Time : 2018-06-01
#include <iostream>
using namespace std;
/// Mapping
/// Time Complexity: O(len(s))
/// Space Complexity: O(len of charset)
class Solution {
public:
bool isIsomorphic(string s, string t) {
if(s.size() != t.size())
return false;
int map[256];
memset(map, -1, sizeof(map));
bool mapped[256];
memset(mapped, false, sizeof(mapped));
for(int i = 0 ; i < s.size() ; i ++){
if(map[s[i]] == -1){
if(mapped[t[i]])
return false;
map[s[i]] = t[i];
mapped[t[i]] = true;
}
else if(map[s[i]] != t[i])
return false;
}
return true;
}
};
void print_bool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
print_bool(Solution().isIsomorphic("egg", "add"));
print_bool(Solution().isIsomorphic("foo", "bar"));
print_bool(Solution().isIsomorphic("paper", "title"));
print_bool(Solution().isIsomorphic("aa", "ab"));
return 0;
}