Find group of strings that are anagrams
<p>This question refers to <a href="http://www.lintcode.com/en/problem/anagrams/" rel="nofollow">this problem on lintcode</a>. I have a working solution, but it takes too long for the huge testcase. I am wondering how can it  be improved? Maybe I can decrease the number of comparisons I make in the outer loop.</p>

<pre><code>class Solution:
    # @param strs: A list of strings
    # @return: A list of strings
    def anagrams(self, strs):
        # write your code here
        ret=set()
        for i in range(0,len(strs)):
            for j in range(i+1,len(strs)):
                if i in ret and j in ret:
                    continue
                if Solution.isanagram(strs[i],strs[j]):
                    ret.add(i)
                    ret.add(j)

        return [strs[i] for i in list(ret)]


    @staticmethod
    def isanagram(s, t):
        if len(s)!=len(t):
            return False
        chars={}
        for i in s:
            if i in chars:
                chars[i]+=1
            else:
                chars[i]=1

        for i in t:
            if i not in chars:
                return False
            else:
                chars[i]-=1
                if chars[i]&lt;0:
                    return False

        for i in chars:
            if chars[i]!=0:
                return False
        return True
</code></pre>

<p><strong>Update:</strong> Just to add, not looking for built-in pythonic solutions such as using <code>Counter</code> which are already optimized. Have added Mike's suggestions, but still exceeding time-limit.</p>

<p>Skip strings you already placed in the set. Don't test them again.</p>

<pre><code># @param strs: A list of strings
# @return: A list of strings
def anagrams(self, strs):
    # write your code here
    ret=set()
    for i in range(0,len(strs)):
        for j in range(i+1,len(strs)):

            # If both anagrams exist in set, there is no need to compare them.
            if i in ret and j in ret:
                continue

            if Solution.isanagram(strs[i],strs[j]):
                ret.add(i)
                ret.add(j)

    return [strs[i] for i in list(ret)]
</code></pre>

<p>You can also do a length comparison in your anagram test before iterating through the letters. Whenever the strings aren't the same length, they can't be anagrams anyway. Also, when a counter in <code>chars</code> reaches -1 when comparing values in t, just return false. Don't iterate through <code>chars</code> again.</p>

<pre><code>@staticmethod
def isanagram(s, t):
    # Test strings are the same length
    if len(s) != len(t):
        return False

    chars={}
    for i in s:
        if i in chars:
            chars[i]+=1
        else:
            chars[i]=1

    for i in t:
        if i not in chars:
            return False
        else:
            chars[i]-=1
            # If this is below 0, return false
            if chars[i] &lt; 0:
                return False

    for i in chars:
        if chars[i]!=0:
            return False
    return True
</code></pre>

python string anagram