simple C# solution linear runtime constant space


  • 1
    W

    The difference between the strings is one character, so I added all of the characters from the larger string, then subtracted from that each character from the shorter string, which leaves just the differing character value. O(m+n) where m is string s length, and n is string t length, and O(1) storage.

    public char FindTheDifference(string s, string t) {
            int sumS = 0;
            foreach(char c in t) {
                sumS += c;
            }
            foreach(char c in s){
                sumS -= c;
            }
            return (char)(sumS);
        }
    

Log in to reply
 

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