Created
October 13, 2018 22:15
-
-
Save mvallebr/a2b1e481899c9a7fb4871c432ea5c022 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/python3 | |
| import math | |
| import os | |
| import random | |
| import re | |
| import sys | |
| def num_inner_anagrams(s, left, right): | |
| al = [0] * (ord('z') - ord('a') + 1) | |
| ar = [0] * (ord('z') - ord('a') + 1) | |
| result = 0 | |
| l = left | |
| r = right | |
| while l < right and r > left: | |
| al[ord(s[l]) - ord('a')] += 1 | |
| ar[ord(s[r]) - ord('a')] += 1 | |
| if al == ar: | |
| result += 1 | |
| l += 1 | |
| r -= 1 | |
| return result | |
| # Complete the sherlockAndAnagrams function below. | |
| def sherlockAndAnagrams(s): | |
| result = 0 | |
| l = len(s) | |
| for i in range(l): | |
| for j in range(i+1, l): | |
| result += num_inner_anagrams(s, i, j) | |
| return result | |
| if __name__ == '__main__': | |
| fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
| q = int(input()) | |
| for q_itr in range(q): | |
| s = input() | |
| result = sherlockAndAnagrams(s) | |
| fptr.write(str(result) + '\n') | |
| fptr.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment