Because the character can be duplicated, so we cannot use HashSet.

This is easy if using bucket:

    public char findTheDifference(String s, String t) {
        int[] buckets = new int[26];
        for(char c: s.toCharArray()){
            buckets[c-'a']++;
        }
        for(char c: t.toCharArray()){
            buckets[c-'a']--;
        }
        for(int i=0;i<buckets.length;i++){
            if(buckets[i]<0)
                return (char)('a'+i);
        }
        return '*';
    }

 

posted on 2022-02-25 06:26  阳光明媚的菲越  阅读(20)  评论(0编辑  收藏  举报