A simple solution in c# using nested loop to find first unique character in a string . Time Complexity is O(n) . Space Complexity is O(n)


  • 0
    U
                var parts = s.ToCharArray();
                var distinct = s.ToCharArray().Distinct();
                int match,matchIndex=0;
                foreach (var item in distinct)
                {
                    match = 0;
                    for (int j = 0;j < parts.Length; j++)
                    {
                        if (item == parts[j])
                        {
                            match++;
                            matchIndex = j;
                        }
                    }
                    if (match == 1)
                        return matchIndex;
                }
                return -1;
    

Log in to reply
 

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