c++ solution slow but it works using sorting


  • 0
    A

    '''
    sort(s.begin(), s.end());
    sort(t.begin(), t.end());
    char result;

    for (int i = 0; i < t.size(); i++)
    {
    	    if (s[i] != t[i])
    		{
    			result = t[i];
    			break;
    		}
    	}
    cout << result << endl;
    

    '''


  • 0
    I

    Anyone know why this JS solution that is the exact same as above does not work?

    
    var findTheDifference = function(s, t) {
        let result;
        s.split('').sort();
        t.split('').sort();
        for (let i = 0; i < t.length; i++){
            if (s[i] != t[i]){
                result = t[i];
                break;
            }
        }
        return result;
    };
    

  • 0
    J

    I think s[i] maybe overflowed when i = t.length-1.


Log in to reply
 

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