using hash table, easy to UNDERSTAND


  • 0
    C
       public static char findTheDifference(String s, String t) {
    	int[] count = new int[26];
    	for (int i = 0; i < s.length(); i++) {
    		count[s.charAt(i) - 'a']++;
    	}
    	for (int i = 0; i < t.length(); i++) {
    		count[t.charAt(i) - 'a']--;
    		if (count[t.charAt(i) - 'a'] < 0) {
    			return t.charAt(i);
    		}
    	}
    	return ' ';
    }

Log in to reply
 

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