-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2283-CheckIfNumberHasEqualDigitCountAndDigitValue.go
More file actions
70 lines (61 loc) · 2.28 KB
/
2283-CheckIfNumberHasEqualDigitCountAndDigitValue.go
File metadata and controls
70 lines (61 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
// 2283. Check if Number Has Equal Digit Count and Digit Value
// 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.
import "fmt"
func digitCount(num string) bool {
mp := make([]int, 10)
for _, v := range num {
mp[v - '0']++
}
for i, v := range num {
if mp[i] != int(v - '0') {
return false
}
}
return true
}
func main() {
// 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.
fmt.Println(digitCount("1210")) // 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.
fmt.Println(digitCount("030")) // false
fmt.Println(digitCount("111111111")) // false
fmt.Println(digitCount("123456789")) // false
}