返回介绍

solution / 1900-1999 / 1941.Check if All Characters Have Equal Number of Occurrences / README_EN

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

1941. Check if All Characters Have Equal Number of Occurrences

中文文档

Description

Given a string s, return true_ if _s_ is a good string, or _false_ otherwise_.

A string s is good if all the characters that appear in s have the same number of occurrences (i.e., the same frequency).

 

Example 1:

Input: s = "abacbc"
Output: true
Explanation: The characters that appear in s are 'a', 'b', and 'c'. All characters occur 2 times in s.

Example 2:

Input: s = "aaabb"
Output: false
Explanation: The characters that appear in s are 'a' and 'b'.
'a' occurs 3 times while 'b' occurs 2 times, which is not the same number of times.

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of lowercase English letters.

Solutions

Solution 1

class Solution:
  def areOccurrencesEqual(self, s: str) -> bool:
    cnt = Counter(s)
    return len(set(cnt.values())) == 1
class Solution {
  public boolean areOccurrencesEqual(String s) {
    int[] cnt = new int[26];
    for (int i = 0; i < s.length(); ++i) {
      ++cnt[s.charAt(i) - 'a'];
    }
    int x = 0;
    for (int v : cnt) {
      if (v > 0) {
        if (x == 0) {
          x = v;
        } else if (x != v) {
          return false;
        }
      }
    }
    return true;
  }
}
class Solution {
public:
  bool areOccurrencesEqual(string s) {
    int cnt[26]{};
    for (char& c : s) {
      ++cnt[c - 'a'];
    }
    int x = 0;
    for (int& v : cnt) {
      if (v) {
        if (!x) {
          x = v;
        } else if (x != v) {
          return false;
        }
      }
    }
    return true;
  }
};
func areOccurrencesEqual(s string) bool {
  cnt := [26]int{}
  for _, c := range s {
    cnt[c-'a']++
  }
  x := 0
  for _, v := range cnt {
    if v > 0 {
      if x == 0 {
        x = v
      } else if x != v {
        return false
      }
    }
  }
  return true
}
function areOccurrencesEqual(s: string): boolean {
  const cnt: number[] = new Array(26).fill(0);
  for (const c of s) {
    ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
  }
  let x = 0;
  for (const v of cnt) {
    if (v) {
      if (!x) {
        x = v;
      } else if (x !== v) {
        return false;
      }
    }
  }
  return true;
}
class Solution {
  /**
   * @param String $s
   * @return Boolean
   */
  function areOccurrencesEqual($s) {
    for ($i = 0; $i < strlen($s); $i++) {
      $hashtable[$s[$i]] += 1;
    }
    $rs = array_unique($hashtable);
    return count($rs) === 1;
  }
}

Solution 2

function areOccurrencesEqual(s: string): boolean {
  const cnt: number[] = new Array(26).fill(0);
  for (const c of s) {
    ++cnt[c.charCodeAt(0) - 'a'.charCodeAt(0)];
  }
  const x = cnt.find(v => v);
  return cnt.every(v => !v || v === x);
}

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

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

发布评论

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