Hello Community!
I wanted to contribute to the community but since I don’t have enough karma points, I want you to upvote only if you like my unofficial editorial on PERMPAL:
My approach:
- The answer will be -1 only when the count of any of 26 characters is odd and there exists more than one such character’s example : abbc, here we can see both ‘a’ and ‘c’ have odd count in the given string.
- In any other case, we will always find a answer, that is palindrome.
- Based on above point, we will deal with two cases :
(A)When only one character comes with odd count and rest other characters with even count.
(B)When all the character in the given string are of even count.
Now the question comes How to solve this?
So it is much clear for case 3(A) that the odd count characters should be in the middle of string in order to make it palindrome, eg abbba n(b)=3 i.e odd
Since the ordering of characters won’t matter in case of even count, we will print index one by one from vector<vector> v(26), which I have used this for storing the index of each character(a-z) in the string. In the output I need to just print the even count characters index ,simultaneously from beginning and end.
Keep in mind that the odd count character should be in the middle.
If you give up, you can see my solution here.
All suggestions are welcome!
Thanks Community,
Happy Coding!