Skip to content

Commit 027d10d

Browse files
committed
Add solution and test-cases for problem 3333
1 parent 8ae5d85 commit 027d10d

File tree

3 files changed

+90
-24
lines changed

3 files changed

+90
-24
lines changed

leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/README.md

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3333.Find the Original Typed String II][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+
Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and **may** press a key for too long, resulting in a character being typed **multiple** times.
5+
6+
You are given a string `word`, which represents the **final** output displayed on Alice's screen. You are also given a **positive** integer `k`.
7+
8+
Return the total number of possible original strings that Alice might have intended to type, if she was trying to type a string of size **at least** `k`.
9+
10+
Since the answer may be very large, return it **modulo** `10^9 + 7`.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: word = "aabbccdd", k = 7
16+
17+
Output: 5
18+
19+
Explanation:
20+
21+
The possible strings are: "aabbccdd", "aabbccd", "aabbcdd", "aabccdd", and "abbccdd".
22+
```
23+
24+
**Example 2:**
25+
1326
```
27+
Input: word = "aabbccdd", k = 8
1428
15-
## 题意
16-
> ...
29+
Output: 1
1730
18-
## 题解
31+
Explanation:
1932
20-
### 思路1
21-
> ...
22-
Find the Original Typed String II
23-
```go
33+
The only possible string is "aabbccdd".
2434
```
2535

36+
**Example 3:**
37+
38+
```
39+
Input: word = "aaabbb", k = 3
40+
41+
Output: 8
42+
```
2643

2744
## 结语
2845

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

3-
func Solution(x bool) bool {
4-
return x
3+
const mod = 1000000007
4+
5+
func Solution(word string, k int) int {
6+
n := len(word)
7+
cnt := 1
8+
var freq []int
9+
10+
for i := 1; i < n; i++ {
11+
if word[i] == word[i-1] {
12+
cnt++
13+
} else {
14+
freq = append(freq, cnt)
15+
cnt = 1
16+
}
17+
}
18+
freq = append(freq, cnt)
19+
20+
ans := 1
21+
for _, o := range freq {
22+
ans = ans * o % mod
23+
}
24+
25+
if len(freq) >= k {
26+
return ans
27+
}
28+
29+
f := make([]int, k)
30+
g := make([]int, k)
31+
f[0] = 1
32+
for i := range g {
33+
g[i] = 1
34+
}
35+
36+
for i := 0; i < len(freq); i++ {
37+
f_new := make([]int, k)
38+
for j := 1; j < k; j++ {
39+
f_new[j] = g[j-1]
40+
if j-freq[i]-1 >= 0 {
41+
f_new[j] = (f_new[j] - g[j-freq[i]-1] + mod) % mod
42+
}
43+
}
44+
g_new := make([]int, k)
45+
g_new[0] = f_new[0]
46+
for j := 1; j < k; j++ {
47+
g_new[j] = (g_new[j-1] + f_new[j]) % mod
48+
}
49+
f, g = f_new, g_new
50+
}
51+
52+
return (ans - g[k-1] + mod) % mod
553
}

leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/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+
inputs string
14+
k int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "aabbccdd", 7, 5},
18+
{"TestCase2", "aabbccdd", 8, 1},
19+
{"TestCase3", "aaabbb", 3, 8},
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.inputs, c.k)
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.inputs, c.k)
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)