Skip to content

Commit 71b301b

Browse files
committed
Sync LeetCode submission Runtime - 4055 ms (58.82%), Memory - 26.1 MB (56.86%)
1 parent c95b9cc commit 71b301b

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<p>Alice is attempting to type a specific string on her computer. However, she tends to be clumsy and <strong>may</strong> press a key for too long, resulting in a character being typed <strong>multiple</strong> times.</p>
2+
3+
<p>You are given a string <code>word</code>, which represents the <strong>final</strong> output displayed on Alice&#39;s screen. You are also given a <strong>positive</strong> integer <code>k</code>.</p>
4+
5+
<p>Return the total number of <em>possible</em> original strings that Alice <em>might</em> have intended to type, if she was trying to type a string of size <strong>at least</strong> <code>k</code>.</p>
6+
7+
<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<div class="example-block">
13+
<p><strong>Input:</strong> <span class="example-io">word = &quot;aabbccdd&quot;, k = 7</span></p>
14+
15+
<p><strong>Output:</strong> <span class="example-io">5</span></p>
16+
17+
<p><strong>Explanation:</strong></p>
18+
19+
<p>The possible strings are: <code>&quot;aabbccdd&quot;</code>, <code>&quot;aabbccd&quot;</code>, <code>&quot;aabbcdd&quot;</code>, <code>&quot;aabccdd&quot;</code>, and <code>&quot;abbccdd&quot;</code>.</p>
20+
</div>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<div class="example-block">
25+
<p><strong>Input:</strong> <span class="example-io">word = &quot;aabbccdd&quot;, k = 8</span></p>
26+
27+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
28+
29+
<p><strong>Explanation:</strong></p>
30+
31+
<p>The only possible string is <code>&quot;aabbccdd&quot;</code>.</p>
32+
</div>
33+
34+
<p><strong class="example">Example 3:</strong></p>
35+
36+
<div class="example-block">
37+
<p><strong>Input:</strong> <span class="example-io">word = &quot;aaabbb&quot;, k = 3</span></p>
38+
39+
<p><strong>Output:</strong> <span class="example-io">8</span></p>
40+
</div>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= word.length &lt;= 5 * 10<sup>5</sup></code></li>
47+
<li><code>word</code> consists only of lowercase English letters.</li>
48+
<li><code>1 &lt;= k &lt;= 2000</code></li>
49+
</ul>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def possibleStringCount(self, word: str, k: int) -> int:
3+
mod = 10**9 + 7
4+
n, cnt = len(word), 1
5+
freq = list()
6+
7+
for i in range(1, n):
8+
if word[i] == word[i - 1]:
9+
cnt += 1
10+
else:
11+
freq.append(cnt)
12+
cnt = 1
13+
freq.append(cnt)
14+
15+
ans = 1
16+
for o in freq:
17+
ans = ans * o % mod
18+
19+
if len(freq) >= k:
20+
return ans
21+
22+
f, g = [1] + [0] * (k - 1), [1] * k
23+
for i in range(len(freq)):
24+
f_new = [0] * k
25+
for j in range(1, k):
26+
f_new[j] = g[j - 1]
27+
if j - freq[i] - 1 >= 0:
28+
f_new[j] = (f_new[j] - g[j - freq[i] - 1]) % mod
29+
g_new = [f_new[0]] + [0] * (k - 1)
30+
for j in range(1, k):
31+
g_new[j] = (g_new[j - 1] + f_new[j]) % mod
32+
f, g = f_new, g_new
33+
return (ans - g[k - 1]) % mod

0 commit comments

Comments
 (0)