Skip to content

Commit c37d9dc

Browse files
authored
Create 1657 Determine if two strings are close.cpp
1 parent 7e75002 commit c37d9dc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
bool closeStrings(string word1, string word2) {
4+
if(word1.length()!=word2.length()) {
5+
return false;
6+
}
7+
8+
unordered_map<char,int> freq1,freq2;
9+
unordered_set<char> chars1(word1.begin(),word1.end()),chars2(word2.begin(),word2.end());
10+
11+
for (char c:word1) freq1[c]++;
12+
for (char c:word2) freq2[c]++;
13+
14+
if (chars1!=chars2) {
15+
return false;
16+
}
17+
18+
vector<int> freqCount1,freqCount2;
19+
for (auto& entry:freq1)
20+
freqCount1.push_back(entry.second);
21+
for (auto& entry:freq2)
22+
freqCount2.push_back(entry.second);
23+
24+
sort(freqCount1.begin(),freqCount1.end());
25+
sort(freqCount2.begin(),freqCount2.end());
26+
return freqCount1 == freqCount2;
27+
}
28+
};

0 commit comments

Comments
 (0)