2022-12-02 20:13:07 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
class Solution {
|
|
|
|
using freq_t = std::array<std::size_t, 26>;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
freq_t freqs(const std::string &word) {
|
|
|
|
freq_t f{0};
|
2022-12-02 20:13:07 +01:00
|
|
|
|
|
|
|
for (auto c : word) {
|
|
|
|
f[c - 'a']++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
int mask(const freq_t &f) {
|
2022-12-02 20:13:07 +01:00
|
|
|
int m = 0;
|
|
|
|
|
|
|
|
for (auto c : f) {
|
|
|
|
m = (m << 1) | (c > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
bool closeStrings(std::string word1, std::string word2) {
|
2022-12-02 20:13:07 +01:00
|
|
|
auto f1 = freqs(word1);
|
|
|
|
auto m1 = mask(f1);
|
|
|
|
std::sort(f1.begin(), f1.end());
|
|
|
|
|
|
|
|
auto f2 = freqs(word2);
|
|
|
|
auto m2 = mask(f2);
|
|
|
|
std::sort(f2.begin(), f2.end());
|
|
|
|
|
|
|
|
return m1 == m2 && f1 == f2;
|
|
|
|
}
|
|
|
|
};
|