Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfcc84f

Browse files
authored
Create Find the Original Typed String II.py
1 parent 1dac1f2 commit cfcc84f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

‎Find the Original Typed String II.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'''
2+
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.
3+
4+
You are given a string word, which represents the final output displayed on Alice's screen. You are also given a positive integer k.
5+
6+
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.
7+
8+
Since the answer may be very large, return it modulo 109 + 7.
9+
'''
10+
11+
12+
class Solution:
13+
MOD = 10**9 + 7
14+
15+
def possibleStringCount(self, word: str, k: int) -> int:
16+
if not word:
17+
return 0
18+
19+
groups = []
20+
count = 1
21+
for i in range(1, len(word)):
22+
if word[i] == word[i - 1]:
23+
count += 1
24+
else:
25+
groups.append(count)
26+
count = 1
27+
groups.append(count)
28+
29+
total = 1
30+
for num in groups:
31+
total = (total * num) % self.MOD
32+
33+
if k <= len(groups):
34+
return total
35+
36+
dp = [0] * k
37+
dp[0] = 1
38+
39+
for num in groups:
40+
new_dp = [0] * k
41+
sum_val = 0
42+
for s in range(k):
43+
if s > 0:
44+
sum_val = (sum_val + dp[s - 1]) % self.MOD
45+
if s > num:
46+
sum_val = (sum_val - dp[s - num - 1] + self.MOD) % self.MOD
47+
new_dp[s] = sum_val
48+
dp = new_dp
49+
50+
invalid = sum(dp[len(groups):k]) % self.MOD
51+
return (total - invalid + self.MOD) % self.MOD

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /