You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/README_EN.md
+33-29
Original file line number
Diff line number
Diff line change
@@ -65,7 +65,11 @@ The indices 0 and 1 both violate the condition, so return false.
65
65
66
66
<!-- solution:start -->
67
67
68
-
### Solution 1
68
+
### Solution 1: Counting + Enumeration
69
+
70
+
We can use an array $\textit{cnt}$ of length $10$ to count the occurrences of each digit in the string $\textit{num}$. Then, we enumerate each digit in the string $\textit{num}$ and check if its occurrence count equals the digit itself. If this condition is satisfied for all digits, we return $\text{true}$; otherwise, we return $\text{false}$.
71
+
72
+
The time complexity is $O(n)$, and the space complexity is $O(|\Sigma|)$. Here, $n$ is the length of the string $\textit{num}$, and $|\Sigma|$ is the range of possible digit values, which is $10$.
69
73
70
74
<!-- tabs:start -->
71
75
@@ -74,8 +78,8 @@ The indices 0 and 1 both violate the condition, so return false.
74
78
```python
75
79
classSolution:
76
80
defdigitCount(self, num: str) -> bool:
77
-
cnt = Counter(num)
78
-
returnall(cnt[str(i)] ==int(v) for i, vinenumerate(num))
81
+
cnt = Counter(int(x) for x innum)
82
+
returnall(cnt[i] ==int(x) for i, xinenumerate(num))
0 commit comments