which code remove the duplicate combination in permutation
<p>I didn't find the obvious difference between two functions below . So the question is , how the second funtion compare and remove duplicate characters .</p>

<p>permutation for non-duplicate characters</p>

<pre><code>def perms(s):        
    if(len(s)==1): return [s]
    result=[]
    for i,v in enumerate(s):
        result += [v+p for p in perms(s[:i]+s[i+1:])]
    return result
perms('abc')
['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
</code></pre>

<p>permutation for duplicate characters</p>

<pre><code>def permutations(string):
    result = set([string])
    if len(string) == 2:
        result.add(string[1] + string[0])
    elif len(string) &gt; 2:
        for i, c in enumerate(string):
            for s in permutations(string[:i] + string[i + 1:]):
                result.add(c + s)
    return list(result)
permutations('aabb'); 
['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa']
</code></pre>

<p><strong>EDIT:</strong></p>

<p>The function return different results when input contains duplicates:</p>

<pre><code>&gt;&gt;&gt; permutations('aabb')
['abab', 'baba', 'bbaa', 'abba', 'aabb', 'baab']
&gt;&gt;&gt; perms('aabb')
['aabb', 'aabb', 'abab', 'abba', 'abab', 'abba', 'aabb', 'aabb', 'abab',
'abba', 'abab', 'abba', 'baab', 'baba', 'baab', 'baba', 'bbaa', 'bbaa', 
'baab', 'baba', 'baab', 'baba', 'bbaa', 'bbaa']
</code></pre>

<p>The difference is very simple. The second function stores results in a set:</p>

<pre><code>result = set([string])
</code></pre>

<p>A set never contains duplicates. If you add a duplicate value to a set, noting happens:</p>

<pre><code>&gt;&gt;&gt; set([1, 2, 3, 2, 3, 2, 1])
set([1, 2, 3])
</code></pre>

<p>In the end, the function creates a list from the set, so the set is not visible from the outside:</p>

<pre><code>return list(result)
</code></pre>

python algorithm recursion