Skip to content

Commit d276a04

Browse files
authored
Merge pull request #1203 from 0xff-dev/3335
Add solution and test-cases for problem 3335
2 parents 534e775 + 0627123 commit d276a04

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed

leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/README.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
11
# [3335.Total Characters in String After Transformations 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` and an integer `t`, representing the number of **transformations** to perform. In one **transformation**, every character in `s` is replaced according to the following rules:
5+
6+
- If the character is `'z'`, replace it with the string `"ab"`.
7+
- Otherwise, replace it with the next character in the alphabet. For example, `'a'` is replaced with `'b'`, `'b'` is replaced with `'c'`, and so on.
8+
9+
Return the **length** of the resulting string after **exactly** `t` transformations.
10+
11+
Since the answer may be very large, return it **modulo** `10^9 + 7`.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: s = "abcyy", t = 2
17+
18+
Output: 7
19+
20+
Explanation:
21+
22+
First Transformation (t = 1):
23+
'a' becomes 'b'
24+
'b' becomes 'c'
25+
'c' becomes 'd'
26+
'y' becomes 'z'
27+
'y' becomes 'z'
28+
String after the first transformation: "bcdzz"
29+
Second Transformation (t = 2):
30+
'b' becomes 'c'
31+
'c' becomes 'd'
32+
'd' becomes 'e'
33+
'z' becomes "ab"
34+
'z' becomes "ab"
35+
String after the second transformation: "cdeabab"
36+
Final Length of the string: The string is "cdeabab", which has 7 characters.
1337
```
1438

15-
## 题意
16-
> ...
39+
**Example 2:**
1740

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Total Characters in String After Transformations I
23-
```go
2441
```
42+
Input: s = "azbk", t = 1
43+
44+
Output: 5
2545
46+
Explanation:
47+
48+
First Transformation (t = 1):
49+
'a' becomes 'b'
50+
'z' becomes "ab"
51+
'b' becomes 'c'
52+
'k' becomes 'l'
53+
String after the first transformation: "babcl"
54+
Final Length of the string: The string is "babcl", which has 5 characters.
55+
```
2656

2757
## 结语
2858

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

3-
func Solution(x bool) bool {
4-
return x
3+
const mod = 1000000007
4+
5+
func Solution(s string, t int) int {
6+
count := [26]int{}
7+
for _, b := range s {
8+
count[b-'a']++
9+
}
10+
for ; t > 0; t-- {
11+
tmp := [26]int{}
12+
// a, b, c, d .... z
13+
// count是前一轮的变化
14+
z := count[25]
15+
for i := 25; i > 0; i-- {
16+
tmp[i] = count[i-1]
17+
}
18+
tmp[0] = z
19+
tmp[1] = (tmp[1] + z) % mod
20+
for i := range 26 {
21+
count[i] = tmp[i]
22+
}
23+
}
24+
ans := 0
25+
for i := range 26 {
26+
ans = (ans + count[i]) % mod
27+
}
28+
return ans
529
}

leetcode/3301-3400/3335.Total-Characters-in-String-After-Transformations-I/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+
tt int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "abcyy", 2, 7},
18+
{"TestCase2", "azbk", 1, 5},
19+
{"TestCase3", "jqktcurgdvlibczdsvnsg", 7517, 79033769},
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.tt)
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",
28+
c.expect, got, c.s, c.tt)
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)