We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

aaabbb is not a palindrome, as aaabbb != bbbaaa. :)
Let me explain the approach a bit further.
Let's say we have a string abcxyz. If it's a palindrome, than a =z , b=y, c = x. So we have a string abccba. So we have a pair of each elements. Which means if the len of the string is even => there is at least one pair of each element hence the count of each element should be even. But if the len of the string is odd it means in the middle of the string there should be one and only one unpaired element. So, the count of ONLY one element should be odd.

s = input()
s = list(s)
liss = [0]26
for char in s:
if(liss[ord(char)-ord('a')]==0):
liss[ord(char)-ord('a')]+=1
else:
liss[ord(char)-ord('a')]-=1
total = sum(map(abs,liss))
if(total<=1):
print("YES")
else:
print("NO")

## Game of Thrones - I

You are viewing a single comment's thread. Return to all comments →

but, what about the string, aaabbb, it is a palindrome with a's odd and b's odd. can someone explane the approche.

aaabbb is not a palindrome, as aaabbb != bbbaaa. :) Let me explain the approach a bit further. Let's say we have a string abcxyz. If it's a palindrome, than a =z , b=y, c = x. So we have a string abccba. So we have a pair of each elements. Which means if the len of the string is even => there is at least one pair of each element hence the count of each element should be even. But if the len of the string is odd it means in the middle of the string there should be one and only one unpaired element. So, the count of ONLY one element should be odd.

thnkuuu ur explaination is really helpful... :)

Then why is my testcases failing here? Does the execution has problem?

Bro your question is wrong,its,aaabbbb

hi String "aaabbb" is not palindrome . in the sample test case string is "aaabbbb" .

s = input() s = list(s) liss = [0]26 for char in s: if(liss[ord(char)-ord('a')]==0): liss[ord(char)-ord('a')]+=1 else: liss[ord(char)-ord('a')]-=1 total = sum(map(abs,liss)) if(total<=1): print("YES") else: print("NO")