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
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

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Employee Management System Using Inheritance in Java

Bit Stuffing Code Implementation in Java