Skip to content

Create 3333. Find the Original Typed String II #831

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

Merged
merged 1 commit into from
Jul 2, 2025
Merged
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
44 changes: 44 additions & 0 deletions 3333. Find the Original Typed String II
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
static constexpr int MOD = 1'000'000'007;
public:
int possibleStringCount(string word, int k) {
vector<int> groups;
for(int i = 0, n = word.size(); i < n; ) {
int j = i+1;
while(j<n && word[j]==word[i]) j++;
groups.push_back(j - i);
i = j;
}

long long total = 1;
for(int g : groups)
total = total * g % MOD;

int m = groups.size();
if(k > (int)groups.size()) {
vector<int> dp(k), newdp(k);
dp[0] = 1;
for(int idx = 0; idx < m; idx++) {
int len = groups[idx];
fill(newdp.begin(), newdp.end(), 0);

long long window = 0;
for(int j = 1; j < k; j++) {
window = (window + dp[j-1]) % MOD;
if(j - 1 - len >= 0)
window = (window - dp[j - 1 - len] + MOD) % MOD;
newdp[j] = window;
}
dp.swap(newdp);
}

long long invalid = 0;
for(int j = 1; j < k; j++)
invalid = (invalid + dp[j]) % MOD;

return int((total - invalid + MOD) % MOD);
}
// If minimum intended length <= #groups, we don't exclude anything
return int(total);
}
};
Loading