Short Palindrome Discussions | Algorithms | HackerRank

Short Palindrome

  • + 0 comments
    def shortPalindrome(s):
      arr1 = [0]*26
      arr2 = [[0]*26 for i in range(26)]
      arr3 = [0]*26
      ans = 0
      for i in range(len(s)):
          idx = ord(s[i]) - ord('a')
          ans += arr3[idx]
          for j in range(26):
              arr3[j] += arr2[j][idx]
          for j in range(26):
              arr2[j][idx] += arr1[j]
          arr1[idx] += 1
      return ans % (10**9+7)