Sort by

recency

|

24 Discussions

|

  • + 0 comments
    def nArModp(n,r,p):
        res = 1
        for i in range(r+1,n+1):
            res= (res*i)%p
        return res
    
    def nTorModp(n,r,p):
        if (n>p) or (r> p-1):
            return nTorModp(n%p,r%(p-1),p)
        if r==0:
            return 1
        if r%2:
            return (n*nTorModp(n,r-1,p))%p
        else:
            return (nTorModp(n,r//2,p)**2)%p
        
        
    def nCmultrModp(arr,p):
        N=sum(arr)
        arr=sorted(arr)
        res =1
        rold=0
        lr = len(arr)
        #denominator
        for i,r in enumerate(arr[:lr-1]):
            res = res * (nTorModp(nArModp(r,rold,p),lr-1-i,p)) % p
            rold=r
        #numerator
        rNum = nArModp(N,arr[lr-1],p)
        return (nTorModp(res,p-2,p)*rNum)%p
        
    
    
    def solve(s):
        counts = list(Counter(s).values())
        counts = [c//2 for c in counts if c > 1]
        return nCmultrModp(counts,10**9+7)
    
  • + 0 comments

    My Solution in Python :

    def solve(s):
        # Write your code here
        num=int(len(s)/2)
    
        ca=math.factorial(num)
    
        cou=Counter(s)
    
        for i in cou:
            ca=ca//math.factorial(int(cou[i]/2))
    
        return (ca%((10**9)+7))
    
  • + 0 comments

    ruby:

    def solve(s)
        counts = s.split('').group_by{|x|x}.values.map{|v| v.size/2}
        (1..counts.sum).reduce(:*)/(counts.map{|c| (1..c).reduce(:*) || 1}.reduce(:*))%(10**9+7)
    end
    
  • + 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)
    
  • + 0 comments

    My testcases 31 to 40 give wrong answer. I'm not sure why. Could someone please help. :)