O(n) C++ solution


  • 0
    M
    class Solution {
    public:
        char findTheDifference(string s, string t) {
            int count[26] = {0};
            
            for(int i = 0; i < t.length(); ++i){
                count[t[i] - 'a']++;
            }
            for(int i = 0; i < s.length(); ++i){
                count[s[i] - 'a']--;
            }
            
            int temp;
            for(int i = 0; i < 26; ++i){
                if(count[i] != 0){
                    temp = i;
                    break;
                }
            }
            return temp + 'a';
        }
    };
    

Log in to reply
 

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