My 8ms Java Solution


  • 0
    S

    Of course it can be refactored; this was written for 5 mins;

     public char findTheDifference(String s, String t) {
        if(s == null || s.length() == 0)
            return t.charAt(0);
        int[] letters = new int[26];
        for(int i = 0; i < s.length(); i++){
            int sPosition = s.charAt(i) - 'a';
            int tPosition = t.charAt(i) - 'a';
            letters[sPosition]++;
            letters[tPosition]--;
        }
        int tPosition = t.charAt(t.length()-1) - 'a';
        letters[tPosition]--;
        char res = 'a';
        for(int i = 0; i < letters.length; i++){
            if(letters[i] == -1){
                res+= i;
                break; 
            }    
        }
        return res;
    }

Log in to reply
 

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