题目来源:
leetcode题目,网址:1897. 重新分配字符使所有字符串都相等 - 力扣(LeetCode)
解题思路:
遍历字符串数组,对字符计数后判断是否所有字符都能被数组长度整除即可。
解题代码:
class Solution {
public boolean makeEqual(String[] words) {
int[] alphabet=new int[26];
for(int i=0;i<words.length;i++){
for(int j=0;j<words[i].length();j++){
alphabet[words[i].charAt(j)-'a']++;
}
}
int n=words.length;
for(int i=0;i<26;i++){
if(alphabet[i]%n!=0){
return false;
}
}
return true;
}
}
总结:
官方题解也是一样的思路:统计字符+整除。
redistribute 重新分配