389. Find the Difference

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 '*';
    }

 

上一篇:APP开发流程详解


下一篇:dolphinscheduler-2.0.1安装