Short Palindrome Discussions | Algorithms | HackerRank

Short Palindrome

  • + 0 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