-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloseStrings.cpp
37 lines (37 loc) · 1015 Bytes
/
closeStrings.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
class Solution {
public:
bool closeStrings(string word1, string word2) {
if (word1.length() != word2.length()) {
return false;
}
set<char> set1(word1.begin(), word1.end());
set<char> set2(word2.begin(), word2.end());
if (set1 != set2){
return false;
}
map<char, int> word1count;
for (char ch : word1) {
word1count[ch]++;
}
vector<int> count1;
for (const auto &pair : word1count) {
count1.push_back(pair.second);
}
sort(count1.begin(), count1.end());
map<char, int> word2count;
for (char ch : word2) {
word2count[ch]++;
}
vector<int> count2;
for (const auto &pair : word2count) {
count2.push_back(pair.second);
}
sort(count2.begin(), count2.end());
if (count1 == count2) {
return true;
}
else {
return false;
}
}
};