c# solution O(n) time, O(n) space, 10 lines


  • 0
    M

    public int FirstUniqChar(string s)
    {
    if (s == null) return -1;
    Dictionary<char, int?> dict = new Dictionary<char, int?>();

    for (int i = 0; i < s.Length; i++) dict[s[i]] = dict.ContainsKey(s[i]) ? (int?)null : i;
    int? result = null;
    foreach (var item in dict.Values) if (result == null || item < result) result = item;
    
    return result == null ? -1 : result.Value;
    

    }


Log in to reply
 

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