list of strings - remove commonalities of common strings
<p>I'm struggling to come up with a way to solve this problem (and how to come up with a name for this question on stackoverflow).</p>

<p>I need to somehow remove commonalities of strings preserving the remainder.  </p>

<p>Given a list such as the following:</p>

<pre><code>l = ('first',
     'first.second',
     'first.second.third',
     'a.b.c',
     'x.y.z',
     'x.y') 
</code></pre>

<p>I'm hoping to have a list output as:</p>

<pre><code>('first',
 'second',
 'third', 
 'a.b.c',
 'x.y',
 'z' ) 
</code></pre>

<p>as you can see, when <code>first.second</code> subtracts <code>first</code> and remains <code>second</code>. <code>first.second</code> is subtracted from <code>first.second.third</code> we get <code>third</code>.  <code>a.b.c</code> doesn't have anything to subtract from itself, so it remains.  <code>x.y</code> is subtracted from <code>x.y.z</code> and <code>z</code> remains.</p>

<p>I'm thinking maybe a <code>sort(key=len)</code> will be part of the solution, as well as some sort of recursion to end up with the string remainder.  I'm hoping for a clean way to remove commonalities of each string in the list. </p>

<p>I've thought about this interesting problem some more and came up with a solution.</p>

<p>The problem is fundamentally tree structured, regardless of which tree-like technique you end up using: </p>

<ul>
<li>an actual tree datatype (which is how I initially solved it, but it was much more verbose)</li>
<li>recursion</li>
<li>simulating recursion using stacks (which is what I ultimately ended doing).</li>
</ul>

<p>I'll use the following list of expanded list of words since it points out some edge cases that make other solutions fail:</p>

<pre><code>li = ['First',
      'FirstSecond',
      'FirstSecondFirst',
      'FirstSecondFirstly',
      'FirstSecondThird',
      'FirstFoo',
      'FirstBarracudaSphyraena',
      'xyz',
      'xy',
      '12345',
      '123',
      '1',
      'FireTruckGarage',
      'FireTruck',
      'Fire']
</code></pre>

<p>The trick is in noticing that, every time there's a potential lengthening of the prefix, we must save the previous prefix on the prefix stack (called <code>prefixes</code> here), which contains all prefixes seen so far that haven't yet been exhausted. After we've done with some of the "deeper" wordsâin the sense of being nodes deeper down the treeâwe may need to backtrack and use an old prefix for some shorter word yet again.</p>

<p>After encountering a word that is not prefixed by the current prefix, we must pop the stack of prefixes until we reach one that <em>does</em> prefix the word and continue from there.</p>

<pre><code>def ambiguate(words):
    output = {}
    prefixes = ['']
    prefix = prefixes[0]

    for item in sorted(set(words)):
        backtracked = False

        while not item.startswith(prefix):
            prefix = prefixes.pop()
            backtracked = True

        # If we have backtracked, we put back the current prefix onto the
        # prefix stack since we may have to use it later on.
        if backtracked:
            prefixes.append(prefix)

        # Construct new output and a new prefix and append it to the
        # prefix stack
        output[item] = item.replace(prefix, '', 1)
        prefix = item
        prefixes.append(prefix)

    return output
</code></pre>

<p>Running:</p>

<pre><code>print(ambiguate(li))
</code></pre>

<p>Yields:</p>

<pre><code>{'1': '1',                                       
 '123': '23',                                    
 '12345': '45',                                  
 'Fire': 'Fire',                                 
 'FireTruck': 'Truck',                           
 'FireTruckGarage': 'Garage',                    
 'First': 'First',                               
 'FirstBarracudaSphyraena': 'BarracudaSphyraena',
 'FirstFoo': 'Foo',                              
 'FirstSecond': 'FirstSecond',                   
 'FirstSecondFirst': 'First',                    
 'FirstSecondFirstly': 'ly',                     
 'FirstSecondFourth': 'Fourth',                  
 'FirstSecondThird': 'FirstSecondThird',         
 'a': 'a',                                       
 'abc': 'bc',                                    
 'xy': 'xy',                                     
 'xyz': 'z'}
</code></pre>

python algorithm