My 17 ms JAVA code, really easy to understand


  • 1

    public class Solution {
    public char findTheDifference(String s, String t) {
    int []array = new int [26];
    char result = 'a';
    for(int i = 0; i < s.length(); i++){
    array[s.charAt(i) - 'a']++;
    }
    for(int j = 0; j < t.length(); j++){
    array[t.charAt(j) - 'a']--;
    if(array[t.charAt(j) - 'a']<0){
    result = (char)(t.charAt(j));
    }
    }
    return result;
    }
    }

    Some times it runs within 12 ms


  • 1

    Also, here is my 6 ms solution, that I think I can beat maybe 99% people.

    public class Solution {
    public char findTheDifference(String s, String t) {
    int n = t.length();
    char c = t.charAt(n - 1);
    for (int i = 0; i < n-1 ; i++) {
    c ^= s.charAt(i) ^ t.charAt(i);

    	}
    	return c;
    }
    

    }


Log in to reply
 

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