Skip to content

Commit 9eebf7b

Browse files
authored
Merge pull request #1347 from 0xff-dev/1625
Add solution and test-cases for problem 1625
2 parents 7709e68 + 988d90b commit 9eebf7b

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed

leetcode/1601-1700/1625.Lexicographically-Smallest-String-After-Applying-Operations/README.md

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
# [1625.Lexicographically Smallest String After Applying Operations][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` of **even length** consisting of digits from `0` to `9`, and two integers `a` and `b`.
5+
6+
You can apply either of the following two operations any number of times and in any order on `s`:
7+
8+
- Add `a` to all odd indices of `s` (**0-indexed**). Digits post `9` are cycled back to `0`. For example, if `s = "3456"` and `a = 5`, `s` becomes `"3951"`.
9+
- Rotate `s` to the right by `b` positions. For example, if `s = "3456"` and `b = 1`, s becomes `"6345"`.
10+
11+
Return the **lexicographically smallest** string you can obtain by applying the above operations any number of times on s.
12+
13+
A string `a` is lexicographically smaller than a string `b` (of the same length) if in the first position where `a` and `b` differ, string a has `a` letter that appears earlier in the alphabet than the corresponding letter in `b`. For example, `"0158"` is lexicographically smaller than `"0190"` because the first position they differ is at the third letter, and `'5'` comes before `'9'`.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: s = "5525", a = 9, b = 2
19+
Output: "2050"
20+
Explanation: We can apply the following operations:
21+
Start: "5525"
22+
Rotate: "2555"
23+
Add: "2454"
24+
Add: "2353"
25+
Rotate: "5323"
26+
Add: "5222"
27+
Add: "5121"
28+
Rotate: "2151"
29+
Add: "2050"
30+
There is no way to obtain a string that is lexicographically smaller than "2050".
1331
```
1432

15-
## 题意
16-
> ...
33+
**Example 2:**
1734

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Lexicographically Smallest String After Applying Operations
23-
```go
2435
```
36+
Input: s = "74", a = 5, b = 1
37+
Output: "24"
38+
Explanation: We can apply the following operations:
39+
Start: "74"
40+
Rotate: "47"
41+
Add: "42"
42+
Rotate: "24"
43+
There is no way to obtain a string that is lexicographically smaller than "24".
44+
```
45+
46+
**Example 3:**
2547

48+
```
49+
Input: s = "0011", a = 4, b = 2
50+
Output: "0011"
51+
Explanation: There are no sequence of operations that will give us a lexicographically smaller string than "0011".
52+
```
2653

2754
## 结语
2855

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, a int, b int) string {
4+
n := len(s)
5+
vis := make([]bool, n)
6+
res := s
7+
s = s + s
8+
for i := 0; !vis[i]; i = (i + b) % n {
9+
vis[i] = true
10+
for j := 0; j < 10; j++ {
11+
kLimit := 0
12+
if b%2 != 0 {
13+
kLimit = 9
14+
}
15+
for k := 0; k <= kLimit; k++ {
16+
// before each accumulation, re-truncate t
17+
t := []byte(s[i : i+n])
18+
for p := 1; p < n; p += 2 {
19+
t[p] = '0' + byte((int(t[p]-'0')+j*a)%10)
20+
}
21+
for p := 0; p < n; p += 2 {
22+
t[p] = '0' + byte((int(t[p]-'0')+k*a)%10)
23+
}
24+
tStr := string(t)
25+
if tStr < res {
26+
res = tStr
27+
}
28+
}
29+
}
30+
}
31+
return res
532
}

leetcode/1601-1700/1625.Lexicographically-Smallest-String-After-Applying-Operations/Solution_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
s string
14+
a, b int
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "5525", 9, 2, "2050"},
18+
{"TestCase2", "74", 5, 1, "24"},
19+
{"TestCase3", "0011", 4, 2, "0011"},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.s, c.a, c.b)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
28+
c.expect, got, c.s, c.a, c.b)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)