检查字符是否相同

55 阅读1分钟

给你一个字符串 s ,如果 s 是一个  字符串,请你返回 true ,否则请返回 false 。

如果 s 中出现过的 所有 字符的出现次数 相同 ,那么我们称字符串 s 是  字符串。

输入: s = "abacbc"
输出: true
解释: s 中出现过的字符为 'a''b''c' 。s 中所有字符均出现 2 次。
class Solution {
    public boolean areOccurrencesEqual(String s) {
        
        Map<Character,Integer> map=new HashMap<>();
        for(char c:s.toCharArray()){
            map.put(c,map.getOrDefault(c,0)+1);
        }
        int occ=s.length()/map.size();
        for(int v:map.values()){
            if(v!=occ)return false;
        }
        return true;
    }
}