返回介绍

solution / 2400-2499 / 2423.Remove Letter To Equalize Frequency / README_EN

发布于 2024-06-17 01:03:06 字数 5669 浏览 0 评论 0 收藏 0

2423. Remove Letter To Equalize Frequency

中文文档

Description

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:

  • The frequency of a letter x is the number of times it occurs in the string.
  • You must remove exactly one letter and cannot choose to do nothing.

 

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:

  • 2 <= word.length <= 100
  • word consists of lowercase English letters only.

Solutions

Solution 1: Counting + Enumeration

First, we use a hash table or an array of length $26$ named $cnt$ to count the number of occurrences of each letter in the string.

Next, we enumerate the $26$ letters. If letter $c$ appears in the string, we decrement its count by one, then check whether the counts of the remaining letters are the same. If they are, return true. Otherwise, increment the count of $c$ by one and continue to enumerate the next letter.

If the enumeration ends, it means that it is impossible to make the counts of the remaining letters the same by deleting one letter, so return false.

The time complexity is $O(n + C^2)$, and the space complexity is $O(C)$. Here, $n$ is the length of the string $word$, and $C$ is the size of the character set. In this problem, $C = 26$.

class Solution:
  def equalFrequency(self, word: str) -> bool:
    cnt = Counter(word)
    for c in cnt.keys():
      cnt[c] -= 1
      if len(set(v for v in cnt.values() if v)) == 1:
        return True
      cnt[c] += 1
    return False
class Solution {
  public boolean equalFrequency(String word) {
    int[] cnt = new int[26];
    for (int i = 0; i < word.length(); ++i) {
      ++cnt[word.charAt(i) - 'a'];
    }
    for (int i = 0; i < 26; ++i) {
      if (cnt[i] > 0) {
        --cnt[i];
        int x = 0;
        boolean ok = true;
        for (int v : cnt) {
          if (v == 0) {
            continue;
          }
          if (x > 0 && v != x) {
            ok = false;
            break;
          }
          x = v;
        }
        if (ok) {
          return true;
        }
        ++cnt[i];
      }
    }
    return false;
  }
}
class Solution {
public:
  bool equalFrequency(string word) {
    int cnt[26]{};
    for (char& c : word) {
      ++cnt[c - 'a'];
    }
    for (int i = 0; i < 26; ++i) {
      if (cnt[i]) {
        --cnt[i];
        int x = 0;
        bool ok = true;
        for (int v : cnt) {
          if (v == 0) {
            continue;
          }
          if (x && v != x) {
            ok = false;
            break;
          }
          x = v;
        }
        if (ok) {
          return true;
        }
        ++cnt[i];
      }
    }
    return false;
  }
};
func equalFrequency(word string) bool {
  cnt := [26]int{}
  for _, c := range word {
    cnt[c-'a']++
  }
  for i := range cnt {
    if cnt[i] > 0 {
      cnt[i]--
      x := 0
      ok := true
      for _, v := range cnt {
        if v == 0 {
          continue
        }
        if x > 0 && v != x {
          ok = false
          break
        }
        x = v
      }
      if ok {
        return true
      }
      cnt[i]++
    }
  }
  return false
}
function equalFrequency(word: string): boolean {
  const cnt: number[] = new Array(26).fill(0);
  for (const c of word) {
    cnt[c.charCodeAt(0) - 97]++;
  }
  for (let i = 0; i < 26; ++i) {
    if (cnt[i]) {
      cnt[i]--;
      let x = 0;
      let ok = true;
      for (const v of cnt) {
        if (v === 0) {
          continue;
        }
        if (x && v !== x) {
          ok = false;
          break;
        }
        x = v;
      }
      if (ok) {
        return true;
      }
      cnt[i]++;
    }
  }
  return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文