First Unique Character in a String
<p>Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1.</p>

<pre><code>first_unique('leetcode')  # 0
first_unique('loveleetcode')  # 2
</code></pre>

<p>I came up with the following solution.  How can I make it more efficient for very long input strings?</p>

<pre><code>def first_unique(self, s):
    if s == '':
        return -1

    for item in s:
        if s.count(item) == 1:
            return s.index(item)
            break

    return -1
</code></pre>

<p>Suave version:</p>

<pre><code>from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):
    pass

def first_unique(s):
    counter = OrderedCounter(s)
    try:
        return counter.values().index(1)
    except ValueError:
        return -1
</code></pre>

<hr>

<p>Weird version:</p>

<pre><code>from collections import OrderedDict

def first_unique(s):
    nah = {0}-{0}  # funny pair of eyes 
    yeah = OrderedDict()
    for i,c in enumerate(s):
        if c not in nah:
            try:
                del yeah[c]
            except KeyError:
                yeah[c] = i
            else:
                nah.add(c)
    return next(yeah.itervalues(), -1)
</code></pre>

python