Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,388 views
in Online Assessments by Expert (46,090 points) | 1,388 views

1 Answer

0 like 0 dislike

Given a string word as input, return the minimum number of characters added into the word to become anagram - become a palindrome.

For example, if word = "apple", it's possible to add two letters 'a' and 'e', which makes the resulting string an anadrome: "appleae" letters can be rearranged to become "apelepa", which is a palindrome.

by Expert (46,090 points)
0 0
//what about this code?
//my intuition is  ,to make a word palindrome we should have even count of characters except which comes in middle of word
so we simply count no of odd occuring characters and we increment all those characters except 1..


int main()
{
    string s="apple";
    unordered_map<char,int>count;
    for(auto i:s)
    {
        count[i]++;
    }
    int res=0;
    for(auto i:count)
    {
        if(i.second%2!=0)
        {
            res++;
        }
    }
    cout<<res-1;
    
}