Java solution easy understand (6ms)


  • 1
    S
    public class Solution {
        public char findTheDifference(String s, String t) {
            int[] letters = new int[26];
            for(char ch : t.toCharArray()) {
                letters[ch - 'a']++;
            }
            for(char ch : s.toCharArray()) {
                letters[ch - 'a']--;
            }
            
            for(int i = 0; i < letters.length; i++) {
                if(letters[i] != 0) {
                    return (char)(i + 'a');
                }
            }
            return t.charAt(t.length());
        }
    }
    

Log in to reply
 

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