返回介绍

solution / 2200-2299 / 2283.Check if Number Has Equal Digit Count and Digit Value / README_EN

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

2283. Check if Number Has Equal Digit Count and Digit Value

中文文档

Description

You are given a 0-indexed string num of length n consisting of digits.

Return true _if for every index _i_ in the range _0 <= i < n_, the digit _i_ occurs _num[i]_ times in _num_, otherwise return _false.

 

Example 1:

Input: num = "1210"
Output: true
Explanation:
num[0] = '1'. The digit 0 occurs once in num.
num[1] = '2'. The digit 1 occurs twice in num.
num[2] = '1'. The digit 2 occurs once in num.
num[3] = '0'. The digit 3 occurs zero times in num.
The condition holds true for every index in "1210", so return true.

Example 2:

Input: num = "030"
Output: false
Explanation:
num[0] = '0'. The digit 0 should occur zero times, but actually occurs twice in num.
num[1] = '3'. The digit 1 should occur three times, but actually occurs zero times in num.
num[2] = '0'. The digit 2 occurs zero times in num.
The indices 0 and 1 both violate the condition, so return false.

 

Constraints:

  • n == num.length
  • 1 <= n <= 10
  • num consists of digits.

Solutions

Solution 1

class Solution:
  def digitCount(self, num: str) -> bool:
    cnt = Counter(num)
    return all(cnt[str(i)] == int(v) for i, v in enumerate(num))
class Solution {
  public boolean digitCount(String num) {
    int[] cnt = new int[10];
    int n = num.length();
    for (int i = 0; i < n; ++i) {
      ++cnt[num.charAt(i) - '0'];
    }
    for (int i = 0; i < n; ++i) {
      if (cnt[i] != num.charAt(i) - '0') {
        return false;
      }
    }
    return true;
  }
}
class Solution {
public:
  bool digitCount(string num) {
    int cnt[10]{};
    for (char& c : num) {
      ++cnt[c - '0'];
    }
    for (int i = 0; i < num.size(); ++i) {
      if (cnt[i] != num[i] - '0') {
        return false;
      }
    }
    return true;
  }
};
func digitCount(num string) bool {
  cnt := [10]int{}
  for _, c := range num {
    cnt[c-'0']++
  }
  for i, v := range num {
    if cnt[i] != int(v-'0') {
      return false
    }
  }
  return true
}
function digitCount(num: string): boolean {
  const n = num.length;
  const count = new Array(10).fill(0);
  for (let i = 0; i < n; i++) {
    count[i] = Number(num[i]);
  }
  for (const c of num) {
    count[c]--;
  }
  return count.every(v => v === 0);
}
impl Solution {
  pub fn digit_count(num: String) -> bool {
    let s = num.as_bytes();
    let n = num.len();
    let mut count = [0; 10];
    for i in 0..n {
      count[i] = s[i] - b'0';
    }
    for c in s {
      count[(c - b'0') as usize] -= 1;
    }
    count.iter().all(|v| *v == 0)
  }
}
bool digitCount(char* num) {
  int count[10] = {0};
  for (int i = 0; num[i]; i++) {
    count[i] = num[i] - '0';
  }
  for (int i = 0; num[i]; i++) {
    count[num[i] - '0']--;
  }
  for (int i = 0; i < 10; i++) {
    if (count[i] != 0) {
      return false;
    }
  }
  return true;
}

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

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

发布评论

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