-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101IsomorphicStrings.cpp
More file actions
45 lines (39 loc) · 1.04 KB
/
Copy path101IsomorphicStrings.cpp
File metadata and controls
45 lines (39 loc) · 1.04 KB
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
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Solution {
public:
bool isIsomorphic(string s, string t) {
if (s.size() != t.size()) return false;
int mapST[256];
int mapTS[256];
for(int i = 0; i < 256; i++){
mapST[i] = -1;
mapTS[i] = -1;
}
for (int i = 0; i < (int)s.size(); i++) {
unsigned char a = (unsigned char)s[i];
unsigned char b = (unsigned char)t[i];
// if both not mapped yet → create mapping
if (mapST[a] == -1 && mapTS[b] == -1) {
mapST[a] = b;
mapTS[b] = a;
}
else {
// mapping exists, must match
if (mapST[a] != b || mapTS[b] != a) {
return false;
}
}
}
return true;
}
};
int main(){
Solution obj;
cout<<boolalpha;
bool result = obj.isIsomorphic("egg","add");
cout<<result;
return 0;
}