educative.io

Educative

This solution could've been written much simpler

why is there even a variable called index when it’s not used anywhere?

Here’s a much better solution:

from collections import defaultdict

def permute_palindrome(st):

char_freq = defaultdict(int)
for ch in st:
    char_freq[ch] += 1
odd_counter = 0
for val in char_freq.values():
    if val % 2 != 0:
        odd_counter += 1

return True if odd_counter <= 1 else False

Course: Grokking Coding Interview Patterns in Python - Learn Interactively
Lesson: Solution: Palindrome Permutation

1 Like

Hello @Soroush_Bassam,

Thank you for reporting this feedback on the provided solution code. The index variable doesn’t serve a purpose in the final solution code but is being used in the previous code widgets that help in a step-by-step solution construction.

We’ve fixed the final solution code under the heading “Just the code.” Feel free to share further suggestions and feedback. We’d be happy to help. Thanks!

Happy learning!