rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

389. 找不同 #60

Open rocksc30 opened 1 year ago

rocksc30 commented 1 year ago
class Solution {
    public char findTheDifference(String s, String t) {
        int[] sa = new int[26];
        int[] ta = new int[26];

        int sLen = s.length();
        for(int i = 0; i < sLen; i++){
            sa[s.charAt(i) - 'a']++;
            ta[t.charAt(i) - 'a']++;
        }
        ta[t.charAt(sLen) - 'a']++;

        for(int i = 0; i < 26; i++){
            if(sa[i] != ta[i]){
                return (char)('a' + i);
            }
        }
        return 'a';
    }
}