Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/2283.html
2283. Check if Number Has Equal Digit Count and Digit Value
- Difficulty: Easy.
- Related Topics: Hash Table, String, Counting.
- Similar Questions: Self Dividing Numbers.
Problem
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.
Solution (Java, C++, Python)
-
class Solution { public boolean digitCount(String num) { int[] cnt = new int[11]; char[] arr = num.toCharArray(); for (char d : arr) { ++cnt[d - '0']; } for (int i = 0; i < arr.length; i++) { if (cnt[i] != arr[i] - '0') { return false; } } return true; } } ############ 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: def digitCount(self, num: str) -> bool: cnt = Counter(num) return all(cnt[str(i)] == int(v) for i, v in enumerate(num)) ############ # 2283. Check if Number Has Equal Digit Count and Digit Value # https://leetcode.com/problems/check-if-number-has-equal-digit-count-and-digit-value/ class Solution: def digitCount(self, num: str) -> bool: count = Counter(num) for i, x in enumerate(num): if count[str(i)] != int(x): 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) } }
Explain:
nope.
Complexity:
- Time complexity : O(n).
- Space complexity : O(n).