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.
importsysMOD=1000000007MAX_N=100000fact=[1]*(MAX_N+1)inv_fact=[1]*(MAX_N+1)defmod_inv(x,mod):returnpow(x,mod-2,mod)foriinrange(2,MAX_N+1):fact[i]=fact[i-1]*i%MODinv_fact[MAX_N]=mod_inv(fact[MAX_N],MOD)foriinrange(MAX_N-1,0,-1):inv_fact[i]=inv_fact[i+1]*(i+1)%MODprefix_freq=[]definitialize(s):globalprefix_freqn=len(s)prefix_freq=[[0]*26for_inrange(n+1)]foriinrange(n):prefix_freq[i+1]=prefix_freq[i][:]prefix_freq[i+1][ord(s[i])-ord('a')]+=1defanswerQuery(l,r):freq=[prefix_freq[r][i]-prefix_freq[l-1][i]foriinrange(26)]even_sum=sum(f// 2 for f in freq)odd_count=sum(1forfinfreqiff%2==1)result=fact[even_sum]forfinfreq:iff// 2 > 0:result=result*inv_fact[f// 2] % MOD returnresult*max(1,odd_count)%MOD
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Palindromes
You are viewing a single comment's thread. Return to all comments →