Skip to content

Commit f650fa4

Browse files
committed
Add solution and test-cases for problem 3461
1 parent f03226b commit f650fa4

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/README.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
11
# [3461.Check If Digits Are Equal in String After Operations I][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits:
5+
6+
- 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.
7+
- Replace `s` with the sequence of newly calculated digits, maintaining the order in which they are computed.
8+
9+
Return `true` if the final two digits in `s` are the **same**; otherwise, return `false`.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: s = "3902"
15+
16+
Output: true
17+
18+
Explanation:
19+
20+
Initially, s = "3902"
21+
First operation:
22+
(s[0] + s[1]) % 10 = (3 + 9) % 10 = 2
23+
(s[1] + s[2]) % 10 = (9 + 0) % 10 = 9
24+
(s[2] + s[3]) % 10 = (0 + 2) % 10 = 2
25+
s becomes "292"
26+
Second operation:
27+
(s[0] + s[1]) % 10 = (2 + 9) % 10 = 1
28+
(s[1] + s[2]) % 10 = (9 + 2) % 10 = 1
29+
s becomes "11"
30+
Since the digits in "11" are the same, the output is true.
1331
```
1432

15-
## 题意
16-
> ...
17-
18-
## 题解
33+
**Example 2:**
1934

20-
### 思路1
21-
> ...
22-
Check If Digits Are Equal in String After Operations I
23-
```go
2435
```
36+
Input: s = "34789"
2537
38+
Output: false
39+
40+
Explanation:
41+
42+
Initially, s = "34789".
43+
After the first operation, s = "7157".
44+
After the second operation, s = "862".
45+
After the third operation, s = "48".
46+
Since '4' != '8', the output is false.
47+
```
2648

2749
## 结语
2850

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) bool {
4+
count := len(s) - 2
5+
bs := []byte(s)
6+
for i := count; i > 0; i-- {
7+
for j := 0; j <= i; j++ {
8+
bs[j] = byte((int(bs[j]-'0')+int(bs[j+1]-'0'))%10) + '0'
9+
}
10+
}
11+
return bs[0] == bs[1]
512
}

leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "3902", true},
17+
{"TestCase2", "34789", false},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)