ZeroJudge A224: Ming-ming Loves Ming-ming

"Palindromes" are a rhetorical device referring to words, phrases, or sentences that read the same forwards and backward.
Through the repeated use of words, expressing a relationship of mutual dependence or constraint between the two.
Example: "racecar"

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
EOF inputs, with one set of test data per line. Each line contains a mix of uppercase and lowercase English letters and punctuation marks, with the total number of characters not exceeding 1000.
There will be no space characters in the inputs.
If it's possible to rearrange the order of the characters such that they form a palindrome, output "yes !", otherwise output "no...".
Note that uppercase and lowercase letters are considered the same, meaning 'A' and 'a' are equivalent. Additionally, please ignore all non-alphabetic characters.
ababa
bbaaa
Level
aaabbbcc
abcdefg
HowAreYouToday
A_man,_a_plan,_a_canal:_Panama.
yes !
yes !
yes !
no…
no…
no…
yes !

Thought Process

Because the question requires it to be "rearranged," we need to use the definition of a palindrome to determine if the string can be rearranged into one. In a palindrome, each character appears in pairs, unless the length of the string is odd, in which case the middle character appears unpaired. Therefore, we can use a map to count the occurrence of each character and use "isalpha(str[i])" and "tolower(str[i])" to determine if the current character is an English letter and force all letters to lowercase to avoid capital case issues. After the loop, iterate from 0 to 26 once to ensure that the map value of each letter is even. If there is more than one letter with an odd count (the case where the middle character appears unpaired), the string cannot be rearranged into a palindrome.

Sample Code-ZeroJudge A224: Ming-ming Loves Ming-ming

#include <iostream>
#include <map>
using namespace std;

int main() {
    string str;
    while (cin >> str)
    {
        map<int, int>MAP;
        for (int i = 0; i<str.size(); i++)
        {
            if (isalpha(str[i])) MAP[tolower(str[i]) - 'a']++;
        }
        int count = 0;
        for (int i = 0; i<26; i++)
        {
            if (MAP[i] % 2 != 0) count += 1;
        }
        if (count > 1) cout << "no..." << endl;
        else cout << "yes !" << endl;
    }
}

//ZeroJudge A224
//Dr. SeanXD

Comments