-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay45.cpp
More file actions
34 lines (26 loc) · 771 Bytes
/
Day45.cpp
File metadata and controls
34 lines (26 loc) · 771 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
class Solution {
public:
bool closeStrings(std::string word1, std::string word2) {
vector<int> freq1(26, 0);
vector<int> freq2(26, 0);
for (char ch : word1) {
freq1[ch - 'a']++;
}
for (char ch : word2) {
freq2[ch - 'a']++;
}
for (int i = 0; i < 26; i++) {
if ((freq1[i] == 0 && freq2[i] != 0) || (freq1[i] != 0 && freq2[i] == 0)) {
return false;
}
}
sort(freq1.begin(), freq1.end());
sort(freq2.begin(), freq2.end());
for (int i = 0; i < 26; i++) {
if (freq1[i] != freq2[i]) {
return false;
}
}
return true;
}
};