-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3333-find-the-original-typed-string-ii.js
More file actions
78 lines (66 loc) · 1.83 KB
/
3333-find-the-original-typed-string-ii.js
File metadata and controls
78 lines (66 loc) · 1.83 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
/**
* 3333. Find the Original Typed String II
* https://leetcode.com/problems/find-the-original-typed-string-ii/
* Difficulty: Hard
*
* 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 109 + 7.
*/
/**
* @param {string} word
* @param {number} k
* @return {number}
*/
var possibleStringCount = function(word, k) {
const MOD = 1e9 + 7;
const groups = [];
let count = 1;
for (let i = 1; i < word.length; i++) {
if (word[i] === word[i - 1]) {
count++;
} else {
groups.push(count);
count = 1;
}
}
groups.push(count);
const n = groups.length;
let totalWays = 1;
for (const groupSize of groups) {
totalWays = (totalWays * groupSize) % MOD;
}
if (k <= n) {
return totalWays;
}
let dp = new Array(k).fill(0);
dp[0] = 1;
for (let i = 0; i < n; i++) {
const newDp = new Array(k).fill(0);
const groupSize = groups[i];
let sum = 0;
for (let j = 0; j < Math.min(groupSize, k); j++) {
sum = (sum + dp[j]) % MOD;
newDp[j + 1] = sum;
}
for (let j = groupSize; j < k; j++) {
sum = (sum + dp[j] - dp[j - groupSize] + MOD) % MOD;
if (j + 1 < k) {
newDp[j + 1] = sum;
}
}
dp = newDp;
}
let invalidWays = 0;
for (let j = 0; j < k; j++) {
invalidWays = (invalidWays + dp[j]) % MOD;
}
return (totalWays - invalidWays + MOD) % MOD;
};