반응형

https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal

 

Redistribute Characters to Make All Strings Equal - LeetCode

Can you solve this real interview question? Redistribute Characters to Make All Strings Equal - You are given an array of strings words (0-indexed). In one operation, pick two distinct indices i and j, where words[i] is a non-empty string, and move any cha

leetcode.com

 

words의 모든 문자의 개수를 구해주고, 각 문자가 words의 길이로 나누어 떨어지는지 확인해줍니다.

class Solution {
public:
    bool makeEqual(vector<string>& words) {
        int cnt[26] = {0};
        for(string& w : words) {
            for(char c : w) {
                cnt[c - 'a']++;
            }
        }

        for(int i=0; i<26; i++) {
            if(cnt[i] % words.size() != 0) {
                return false;
            }
        }
        return true;
    }
};
반응형

+ Recent posts