LeetCode-in-Java

2423. Remove Letter To Equalize Frequency

Easy

You are given a 0-indexed string word, consisting of lowercase English letters. You need to select one index and remove the letter at that index from word so that the frequency of every letter present in word is equal.

Return true if it is possible to remove one letter so that the frequency of all letters in word are equal, and false otherwise.

Note:

Example 1:

Input: word = “abcc”

Output: true

Explanation: Select index 3 and delete it: word becomes “abc” and each character has a frequency of 1.

Example 2:

Input: word = “aazz”

Output: false

Explanation: We must delete a character, so either the frequency of “a” is 1 and the frequency of “z” is 2, or vice versa. It is impossible to make all present letters have equal frequency.

Constraints:

Solution

@SuppressWarnings("java:S135")
public class Solution {
    private boolean isValidWord(int[] arr) {
        int temp = 0;
        for (int j = 0; j < 26; j++) {
            if (arr[j] == 0) {
                continue;
            }
            if (temp == 0) {
                temp = arr[j];
                continue;
            }
            if (arr[j] != temp) {
                return false;
            }
        }
        return true;
    }

    public boolean equalFrequency(String word) {
        boolean ans = false;
        // frequency array
        int[] arr = new int[26];
        for (int i = 0; i < word.length(); i++) {
            arr[word.charAt(i) - 'a'] += 1;
        }
        for (int i = 0; i < 26; i++) {
            if (arr[i] != 0) {
                arr[i] -= 1;
                if (isValidWord(arr)) {
                    ans = true;
                    break;
                }
                arr[i] += 1;
            }
        }
        return ans;
    }
}