First Unique Character in a String
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1
class Solution(object):
def firstUniqChar(self, s):
hset = collections.Counter(s);
# Traverse the string from the beginning...
for idx in range(len(s)):
# If the count is equal to 1, it is the first distinct character in the list.
if hset[s[idx]] == 1:
return idx
return -1 # If no character appeared exactly once...
Related Problems
You are given a string s. You can convert s to a palindrome by adding characters in front of it.
Return the shortest palindrome you can find by performing this transformation.
Given a string s, find the length of the longest substring without repeating characters.
Given a string S and an integer K, return the length of the longest substring of S that contains at most K distinct characters.
Given an integer array nums, you need to find one continuous subarray such that if you only sort this subarray in non-decreasing order, then the whole array will be sorted in non-decreasing order.
Return the shortest such subarray and output its length.