-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3461-CheckIfDigitsAreEqualInStringAfterOperationsI.go
More file actions
104 lines (93 loc) · 3.2 KB
/
3461-CheckIfDigitsAreEqualInStringAfterOperationsI.go
File metadata and controls
104 lines (93 loc) · 3.2 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
// 3461. Check If Digits Are Equal in String After Operations I
// You are given a string s consisting of digits.
// Perform the following operation repeatedly until the string has exactly two digits:
// 1. For each pair of consecutive digits in s, starting from the first digit, calculate a new digit as the sum of the two digits modulo 10.
// 2. Replace s with the sequence of newly calculated digits, maintaining the order in which they are computed.
// Return true if the final two digits in s are the same; otherwise, return false.
// Example 1:
// Input: s = "3902"
// Output: true
// Explanation:
// Initially, s = "3902"
// First operation:
// (s[0] + s[1]) % 10 = (3 + 9) % 10 = 2
// (s[1] + s[2]) % 10 = (9 + 0) % 10 = 9
// (s[2] + s[3]) % 10 = (0 + 2) % 10 = 2
// s becomes "292"
// Second operation:
// (s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
// (s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
// s becomes "11"
// Since the digits in "11" are the same, the output is true.
// Example 2:
// Input: s = "34789"
// Output: false
// Explanation:
// Initially, s = "34789".
// After the first operation, s = "7157".
// After the second operation, s = "862".
// After the third operation, s = "48".
// Since '4' != '8', the output is false.
// Constraints:
// 3 <= s.length <= 100
// s consists of only digits.
import "fmt"
func hasSameDigits(s string) bool {
arr, index := []byte(s), 0
for len(arr) > 2 {
index = 0
for i := 1; i < len(arr); i++ {
arr[index] = (byte(int(arr[i-1] - '0') + int(arr[i] - '0')) % 10) + '0'
index++
}
arr = arr[:len(arr) - 1]
}
return arr[0] == arr[1]
}
func hasSameDigits1(s string) bool {
arr := []byte(s)
for i := len(arr) - 1; i > 1; i-- {
for j := 0; j < i; j++ {
sum := (arr[j] - '0') + (arr[j + 1] - '0')
arr[j] = (sum % 10) + '0'
}
}
return arr[0] == arr[1]
}
func main() {
// Example 1:
// Input: s = "3902"
// Output: true
// Explanation:
// Initially, s = "3902"
// First operation:
// (s[0] + s[1]) % 10 = (3 + 9) % 10 = 2
// (s[1] + s[2]) % 10 = (9 + 0) % 10 = 9
// (s[2] + s[3]) % 10 = (0 + 2) % 10 = 2
// s becomes "292"
// Second operation:
// (s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
// (s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
// s becomes "11"
// Since the digits in "11" are the same, the output is true.
fmt.Println(hasSameDigits("3902")) // true
// Example 2:
// Input: s = "34789"
// Output: false
// Explanation:
// Initially, s = "34789".
// After the first operation, s = "7157".
// After the second operation, s = "862".
// After the third operation, s = "48".
// Since '4' != '8', the output is false.
fmt.Println(hasSameDigits("34789")) // false
fmt.Println(hasSameDigits("123456789")) // false
fmt.Println(hasSameDigits("987654321")) // false
fmt.Println(hasSameDigits("1024")) // false
fmt.Println(hasSameDigits1("3902")) // true
fmt.Println(hasSameDigits1("34789")) // false
fmt.Println(hasSameDigits1("123456789")) // false
fmt.Println(hasSameDigits1("987654321")) // false
fmt.Println(hasSameDigits1("1024")) // false
}