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>I would use a for loop to iterate the <code>String</code> from the beginning and at each index, I would check if the rest of the <code>String</code> has the character at the current index by using Substring.</p>

<p>Try this:</p>

<pre><code>def firstUniqChar(word):
for i in range(0,len(word)):  ## iterate through the string
    if not word[i] in word[i+1:len(word)]: ## check if the rest contains that char
        index=i
        break
return index
</code></pre>

<p>Hope it helps!</p>

python