-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
186 lines (169 loc) · 4.29 KB
/
main.go
File metadata and controls
186 lines (169 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Source: https://leetcode.com/problems/find-the-original-typed-string-ii
// Title: Find the Original Typed String II
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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: 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:
// The only possible string is `"aabbccdd"`.
// ```
//
// **Example 3:**
//
// ```
// Input: word = "aaabbb", k = 3
// Output: 8
// ```
//
// **Constraints:**
//
// - `1 <= word.length <= 5 * 10^5`
// - `word` consists only of lowercase English letters.
// - `1 <= k <= 2000`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
const modulo = int(1e9 + 7)
// Use DP, O(n + k^3), TLE
//
// First convert the word into chunks of characters.
// For example, abbcccdddd is represents as [1, 2, 3, 4] (repeated lengths).
//
// Define DP(i, j) be the number of words using first i chunks with length j.
func possibleStringCount(word string, k int) int {
n := len(word)
if k > n {
return 0
}
// Convert to chunk
var chunks []int
chunks = append(chunks, 1)
for i := 1; i < n; i++ {
if word[i] == word[i-1] {
chunks[len(chunks)-1]++
} else {
chunks = append(chunks, 1)
}
}
// Count total
ans := 1
for _, chunk := range chunks {
ans *= chunk
ans %= modulo
}
// Check
k -= len(chunks)
if k <= 0 {
return ans
}
// DP
curr := make([]int, k)
curr[0] = 1
for _, chunk := range chunks {
next := make([]int, k)
for j := range chunk {
for i := range k - j {
if curr[i] == 0 {
break
}
next[i+j] += curr[i]
next[i+j] %= modulo
}
}
curr = next
}
for _, count := range curr {
ans += modulo - count
ans %= modulo
}
return ans % modulo
}
// Use DP, O(n + k^2)
//
// First convert the word into chunks of characters.
// For example, abbcccdddd is represents as C = [1, 2, 3, 4] (repeated lengths).
//
// Note that we can subtract k by len(C) and C[i] by 1,
// since each chunk must have at least one character.
//
// Define F(l, i) be the number of words using first l chunks with length i.
// We have F(0, 0) = 1 and F(l, i) = Sum[j = 0 to C(l)-1] F(l-1, i-j)
// Note that we only need to compute for i < (k - len(C))
//
// This DP has O(k^3) complexity.
// However, we only need to compute the sum of the last l.
//
// Define G(l, i) = Sum[j = 0 to i] F(l, j)
// Then F(l, i) = Sum[j = 0 to C(l)-1] F(l-1, i-j)
// . = Sum[j = i-C(l)+1 to i] F(l-1, j)
// . = G(l-1, i) - G(l-1, i-C(l))
// This reduce the complexity to O(k^2)
func possibleStringCount2(word string, k int) int {
n := len(word)
if k > n {
return 0
}
// Convert to chunk
var chunks []int
chunks = append(chunks, 1)
for i := 1; i < n; i++ {
if word[i] == word[i-1] {
chunks[len(chunks)-1]++
} else {
chunks = append(chunks, 1)
}
}
// Count total
ans := 1
for _, chunk := range chunks {
ans *= chunk
ans %= modulo
}
// Check
k -= len(chunks)
if k <= 0 {
return ans
}
// DP
currG := make([]int, k)
for i := range k {
currG[i] = 1
}
for _, chunk := range chunks {
nextF := make([]int, k)
nextG := make([]int, k)
for i := range k {
nextF[i] = currG[i]
if i-chunk >= 0 {
nextF[i] = (currG[i] - currG[i-chunk] + modulo) % modulo
}
}
nextG[0] = nextF[0]
for i := range k - 1 {
nextG[i+1] = (nextG[i] + nextF[i+1]) % modulo
}
currG = nextG
}
return (ans - currG[k-1] + modulo) % modulo
}