其他分享
首页 > 其他分享> > 389. Find the Difference

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

 

标签:String,int,buckets,char,toCharArray,389,Difference,Find
来源: https://www.cnblogs.com/feiflytech/p/15934307.html