두 문자열 데이터를 받고 해당 데이터들이 Close인지 아닌지 판단하는 문제
Operation 1: Swap any two existing characters.
For example, abcde -> aecdb
Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)
Close 조건은 한 문자열을 위의 두 operation을 사용하여 다른 문자열과 같도록 만들 수 있다면 true이다.
class Solution {
public:
bool closeStrings(string word1, string word2) {
int length = word1.size();
if (length != word2.size())
{
return false;
}
unordered_map<char, int> table1{};
unordered_map<char, int> table2{};
for (int i = 0; i < length; i++)
{
table1[word1[i]]++;
table2[word2[i]]++;
}
length = table1.size();
if (length != table2.size())
{
return false;
}
map<int, int> frequencyTable1{};
map<int, int> frequencyTable2{};
{
auto it1 = table1.begin();
auto it2 = table2.begin();
for (int i = 0; i < length; i++)
{
if (table2.count(it1->first) == 0
|| table1.count(it2->first) == 0)
{
return false;
}
frequencyTable1[it1->second]++;
frequencyTable2[it2->second]++;
it1++;
it2++;
}
}
length = frequencyTable1.size();
if (length != frequencyTable2.size())
{
return false;
}
{
auto it1 = frequencyTable1.begin();
auto it2 = frequencyTable2.begin();
for (int i = 0; i < length; i++)
{
if ((it1->first != it2->first)
|| (it1->second != it2->second))
{
return false;
}
it1++;
it2++;
}
}
return true;
}
};