Skip to content

Add solution and test-cases for problem 3333 #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions leetcode/3301-3400/3333.Find-the-Original-Typed-String-II/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
# [3333.Find the Original Typed String II][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
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.

You are given a string `word`, which represents the **final** output displayed on Alice's screen. You are also given a **positive** integer `k`.

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`.

Since the answer may be very large, return it **modulo** `10^9 + 7`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: word = "aabbccdd", k = 7

Output: 5

Explanation:

The possible strings are: "aabbccdd", "aabbccd", "aabbcdd", "aabccdd", and "abbccdd".
```

**Example 2:**

```
Input: word = "aabbccdd", k = 8

## 题意
> ...
Output: 1

## 题解
Explanation:

### 思路1
> ...
Find the Original Typed String II
```go
The only possible string is "aabbccdd".
```

**Example 3:**

```
Input: word = "aaabbb", k = 3

Output: 8
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
package Solution

func Solution(x bool) bool {
return x
const mod = 1000000007

func Solution(word string, k int) int {
n := len(word)
cnt := 1
var freq []int

for i := 1; i < n; i++ {
if word[i] == word[i-1] {
cnt++
} else {
freq = append(freq, cnt)
cnt = 1
}
}
freq = append(freq, cnt)

ans := 1
for _, o := range freq {
ans = ans * o % mod
}

if len(freq) >= k {
return ans
}

f := make([]int, k)
g := make([]int, k)
f[0] = 1
for i := range g {
g[i] = 1
}

for i := 0; i < len(freq); i++ {
f_new := make([]int, k)
for j := 1; j < k; j++ {
f_new[j] = g[j-1]
if j-freq[i]-1 >= 0 {
f_new[j] = (f_new[j] - g[j-freq[i]-1] + mod) % mod
}
}
g_new := make([]int, k)
g_new[0] = f_new[0]
for j := 1; j < k; j++ {
g_new[j] = (g_new[j-1] + f_new[j]) % mod
}
f, g = f_new, g_new
}

return (ans - g[k-1] + mod) % mod
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs string
k int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "aabbccdd", 7, 5},
{"TestCase2", "aabbccdd", 8, 1},
{"TestCase3", "aaabbb", 3, 8},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.k)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.k)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading