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.
defshortPalindrome(s):upper=1000000007# css1[ord('x') - ord('a')] is the number of subsequences xcss1=[0for_inrange(26)]# css2[ord('x') - ord('a')][ord('y') - ord('a')] is the number of ss xycss2=[[0for_inrange(26)]for_inrange(26)]# css3[ord('x') - ord('a')] is the count of all ss xaa, xbb, xcc, ...css3=[0for_inrange(26)]ans=0forcins:k=ord(c)-ord('a')ans=(ans+css3[k])%upperforiinrange(26):css3[i]=(css3[i]+css2[i][k])%upperforjinrange(26):css2[j][k]=(css2[j][k]+css1[j])%uppercss1[k]=(css1[k]+1)%upperreturnans%upper
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Short Palindrome
You are viewing a single comment's thread. Return to all comments →