두 글자로 된 문자열들을 받고 해당 문자열들을 조합하여
가장 긴 Palindrome 문자열의 길이를 구하는 문제
class Solution {
public:
int longestPalindrome(vector<string>& words) {
unordered_map<string, int> dataMap{};
int counter{0};
string pair{"12"};
bool selfPalindrome{false};
int selfPalindromeCounter{0};
for (int i = 0; i < words.size(); i++)
{
selfPalindrome = false;
pair.front() = words[i].back();
pair.back() = words[i].front();
if (words[i].back() == words[i].front())
{
selfPalindrome = true;
}
if (0 < dataMap[pair])
{
if (selfPalindrome)
{
selfPalindromeCounter--;
}
dataMap[pair]--;
counter++;
}
else
{
if (selfPalindrome)
{
selfPalindromeCounter++;
}
dataMap[words[i]]++;
}
}
counter <<= 2;
if (0 < selfPalindromeCounter)
{
counter += 2;
}
return counter;
}
};