A C# solution, can it be faster?


  • 0
    L
    public int FirstUniqChar(string s)
    {
    	var list = new List<char>(26);
    	int[] flag = new int[26];
    	char[] ch_s = s.ToCharArray();
    	foreach (var a in ch_s)
    	{
    		flag[a - 'a']++;
    	}
    
    	char tag;
    	for (int i = 0; i < 26; i++)
    	{
    		if (flag[i] == 1)
    		{
    			tag = (char)('a' + i);
    			list.Add(tag);
    		}
    	}
    
    	int index = s.Length;
    	if (list.Count != 0)
    	{
    		foreach (var a in list)
    		{
    			index = s.IndexOf(a) < index ? s.IndexOf(a) : index;
    		}
    		return index;
    	}
    	return -1;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.