• + 1 comment

    Well, how can u make a palindrome by suffling the string? At most one letter can be odd, else You can't. *so return -1 for such cases.* Now move on to the logic. first u need to make a frequency array and know that which letter came how many times *Now, to deal with the permutation. You have to worry about the left part, because if you know the left part, right one will be fixed, it will be just reversed* So, you have total length N, but u have to deal only half, so number of blanks to be filled with rotations = factorial(N/2) *Make sure to take only int of N/2* Now, you have some letters to filled in N/2 blanks, but since each letter has its frequency, and we are caring only half side, so we will care only half of each letters count *for example, if 'a' came 10 time, and 'b' came 5 times, we care only 5, and 2 respectively (Note, since we have no more than 1 letter odd, the odd part of letter will be fixed in the middle for forever, thus we never gonna count on odd here)* ans = factorial(N/2)/factorial(each letters count/2) **for example. if count of 'a' is 10, 'b' is '6' and 'c' is '3' time in a string then, ans = 9!/(5!*3!1!)*

    # Pythonic Way
    def countPalin(s):
        counter = defaultdict(int)
        for item in s:
            counter[item] += 1
        n = len(s)//2
        ans = factorial(n)    
        for val in counter.values():
            ans //= factorial(val//2)        
        return (ans % MOD)