-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem87.cpp
More file actions
49 lines (39 loc) · 912 Bytes
/
problem87.cpp
File metadata and controls
49 lines (39 loc) · 912 Bytes
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
#include <bits/stdc++.h>
using namespace std;
class SecretMessageDecoder {
string s, t;
public:
void readInput() {
getline(cin, s);
getline(cin, t);
}
bool isValid() {
if (s.length() != t.length()) return false;
unordered_map<char, char> s_to_t;
unordered_map<char, char> t_to_s;
for (int i = 0; i < s.length(); ++i) {
char sc = s[i];
char tc = t[i];
if (s_to_t.count(sc)) {
if (s_to_t[sc] != tc) return false; // violates same char mapping
} else {
s_to_t[sc] = tc;
}
if (t_to_s.count(tc)) {
if (t_to_s[tc] != sc) return false; // violates unique target
} else {
t_to_s[tc] = sc;
}
}
return true;
}
void display() {
cout << (isValid() ? "true" : "alse") << endl;
}
};
int main() {
SecretMessageDecoder decoder;
decoder.readInput();
decoder.display();
return 0;
}