C# - use counts array, sub optimal, XOR is better


  • 0
    public char FindTheDifference(string s, string t) 
    {
        int[] counts = new int[26];
        foreach (char c in t)
        {
            counts[c - 'a']++;
        }
        foreach (char c in s)
        {
            counts[c - 'a']--;
        }
        
        char x = '#';
        for (int i = 0; i < 26; i++)
        {
            if (counts[i] == 1)
            {
                x = (char)('a' + i);
                break;
            }
        }
        return x;
    }

Log in to reply
 

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