You are viewing a single comment's thread. Return to all comments →
my solution
def shortPalindrome(s): charCount = {} pairCount = {} pairHash = {} totalCount = 0 MOD = 10**9 + 7 for char in s: charIndex = ord(char) - ord('a') pairIndex = charIndex totalCount = (totalCount + pairCount.get(charIndex, 0)) % MOD for pairChar in range(26): pairCount[pairChar] = (pairCount.get(pairChar, 0) + pairHash.get(pairIndex, 0)) % MOD pairHash[pairIndex] = (pairHash.get(pairIndex, 0) + charCount.get(pairChar, 0)) % MOD pairIndex += 26 charCount[charIndex] = charCount.get(charIndex, 0) + 1 return totalCount
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 →
my solution