-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3340-CheckBalancedString.go
More file actions
52 lines (44 loc) · 1.56 KB
/
3340-CheckBalancedString.go
File metadata and controls
52 lines (44 loc) · 1.56 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
package main
// 3340. Check Balanced String
// You are given a string num consisting of only digits.
// A string of digits is called balanced if the sum of the digits at even indices is equal to the sum of digits at odd indices.
// Return true if num is balanced, otherwise return false.
// Example 1:
// Input: num = "1234"
// Output: false
// Explanation:
// The sum of digits at even indices is 1 + 3 == 4, and the sum of digits at odd indices is 2 + 4 == 6.
// Since 4 is not equal to 6, num is not balanced.
// Example 2:
// Input: num = "24123"
// Output: true
// Explanation:
// The sum of digits at even indices is 2 + 1 + 3 == 6, and the sum of digits at odd indices is 4 + 2 == 6.
// Since both are equal the num is balanced.
// Constraints:
// 2 <= num.length <= 100
// num consists of digits only
import "fmt"
func isBalanced(num string) bool {
res := make([]int, 2)
for i, v := range num {
res[i % 2] += int(v - '0')
}
return res[0] == res[1]
}
func main() {
// Example 1:
// Input: num = "1234"
// Output: false
// Explanation:
// The sum of digits at even indices is 1 + 3 == 4, and the sum of digits at odd indices is 2 + 4 == 6.
// Since 4 is not equal to 6, num is not balanced.
fmt.Println(isBalanced("1234")) // false
// Example 2:
// Input: num = "24123"
// Output: true
// Explanation:
// The sum of digits at even indices is 2 + 1 + 3 == 6, and the sum of digits at odd indices is 4 + 2 == 6.
// Since both are equal the num is balanced.
fmt.Println(isBalanced("24123")) // true
}