C# accepted solution


  • 0
    N
    public class Solution {
        public int FirstUniqChar(string s) 
        {
    
            Dictionary<char,int> dict = new Dictionary<char,int>();
            foreach (char c in s)
            {
                if(dict.ContainsKey(c))
                {
                    dict[c]++;
                }
                else
                {
                    dict.Add(c,1);
                }
    
            }
    
            char uniqueChar = dict.Keys.FirstOrDefault(i => dict[i] == 1); 
    
            return uniqueChar!=null? s.IndexOf(uniqueChar) : -1;
        }
    }
    

Log in to reply
 

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