Hackerrank Sherlock and Anagram Solution
This is the solution to the Sherlock and Anagram problem found in the strings section of the algorithm domain.
Screenshot
The Code
Have any Feedback, drop them here !
Screenshot
The Code
import collections def solve(x): d = collections.defaultdict(int) lst = list(x) n = len(lst) for i in xrange(n): for l in xrange(1, n-i+1): sub = lst[i: i+l] sub.sort() d["".join(sub)] += 1 s = 0 for v in d.values(): s += v*(v-1)/2 return s for t in xrange(input()): print solve(map(str,raw_input()))
Have any Feedback, drop them here !
Comments
Post a Comment