From f650fa4c89ccc66e5fa378eedc898d065baef91c Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 23 Oct 2025 09:42:06 +0800 Subject: [PATCH] Add solution and test-cases for problem 3461 --- .../README.md | 48 ++++++++++++++----- .../Solution.go | 11 ++++- .../Solution_test.go | 11 ++--- 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/README.md b/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/README.md index ce9f8a897..4f9ad9886 100755 --- a/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/README.md +++ b/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/README.md @@ -1,28 +1,50 @@ # [3461.Check If Digits Are Equal in String After Operations I][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a string `s` consisting of digits. Perform the following operation repeatedly until the string has **exactly** two digits: + +- 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. +- 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: a = "11", b = "1" -Output: "100" +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:** -### 思路1 -> ... -Check If Digits Are Equal in String After Operations I -```go ``` +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. +``` ## 结语 diff --git a/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution.go b/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution.go index d115ccf5e..a8a4167e2 100644 --- a/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution.go +++ b/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution.go @@ -1,5 +1,12 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) bool { + count := len(s) - 2 + bs := []byte(s) + for i := count; i > 0; i-- { + for j := 0; j <= i; j++ { + bs[j] = byte((int(bs[j]-'0')+int(bs[j+1]-'0'))%10) + '0' + } + } + return bs[0] == bs[1] } diff --git a/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution_test.go b/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution_test.go index 14ff50eb4..11ce16f78 100644 --- a/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution_test.go +++ b/leetcode/3401-3500/3461.Check-If-Digits-Are-Equal-in-String-After-Operations-I/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "3902", true}, + {"TestCase2", "34789", false}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }