Skip to content

Instantly share code, notes, and snippets.

@Roger-Wu
Last active July 26, 2016 08:44
Show Gist options
  • Save Roger-Wu/cca30012cab2bd207bbeaf2235d586e0 to your computer and use it in GitHub Desktop.
Save Roger-Wu/cca30012cab2bd207bbeaf2235d586e0 to your computer and use it in GitHub Desktop.
"""
Problem Description:
https://www.hackerrank.com/contests/world-codesprint-5/challenges/short-palindrome
must compile with pypy 3
"""
MOD = 10**9 + 7
s = input().strip()
# count how many combinations so far
a = [0]*26
ab = [[0]*26 for i in range(26)]
abb = [[0]*26 for i in range(26)]
abba = [[0]*26 for i in range(26)]
for c in s:
c2 = ord(c) - 97 # ord(c) - ord('a')
for c1 in range(26):
abba[c2][c1] += abb[c2][c1]
abb[c1][c2] += ab[c1][c2]
ab[c1][c2] += a[c1]
a[c2] += 1
print(sum(map(sum, abba)) % MOD)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment