-
Notifications
You must be signed in to change notification settings - Fork 1
/
IsomorphicStrings.cpp
51 lines (40 loc) · 1.13 KB
/
IsomorphicStrings.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
// https://www.techiedelight.com/isomorphic-strings/
// https://www.geeksforgeeks.org/check-if-two-given-strings-are-isomorphic-to-each-other/
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
bool isIsomorphic(std::string X, std::string Y)
{
if (X.size() != Y.size()) {
return false;
}
std::unordered_map<char, char> map;
std::unordered_set<char> set;
for (int i = 0; i < X.size(); ++i) {
char x = X[i];
char y = Y[i];
if (map.find(x) != map.end()) {
if (map[x] != y) {
return false;
}
} else {
if (set.find(y) != set.end()) {
return false;
}
set.insert(y);
map[x] = y;
}
}
return true;
}
int main()
{
std::string X = "ACAB";
std::string Y = "XCXY";
std::cout << X << " and " << Y << " are isomorphic? " << std::boolalpha << isIsomorphic(X, Y) << "\n";
X = "banana";
Y = "cololo";
std::cout << X << " and " << Y << " are isomorphic? " << std::boolalpha << isIsomorphic(X, Y) << "\n";
return 0;
}