3333. Find the Original Typed String II #1878
-
Topics: 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 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 Since the answer may be very large, return it modulo Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to determine the number of possible original strings Alice might have intended to type, given the final output string displayed on her screen and a positive integer Approach
Let's implement this solution in PHP: 3333. Find the Original Typed String II <?php
/**
* @param String $word
* @param Integer $k
* @return Integer
*/
function possibleStringCount($word, $k) {
$MOD = 1000000007;
$runs = $this->getRuns($word);
$n_runs = count($runs);
$total_ways = 1;
foreach ($runs as $len) {
$total_ways = (int)(((float)$total_ways * $len) % $MOD);
}
if ($k <= $n_runs) {
return $total_ways;
}
$dp = array_fill(0, $k, 0);
$dp[0] = 1;
$min_index = 0;
foreach ($runs as $l) {
if ($min_index >= $k) {
break;
}
$prefix = array_fill(0, $k, 0);
$prefix[0] = $dp[0];
for ($i = 1; $i < $k; $i++) {
$prefix[$i] = ($prefix[$i-1] + $dp[$i]) % $MOD;
}
$new_dp = array_fill(0, $k, 0);
$start_t = $min_index + 1;
if ($start_t < $k) {
for ($t = $start_t; $t < $k; $t++) {
$term1 = $prefix[$t-1];
$term2 = 0;
$prev_index = $t - $l - 1;
if ($prev_index >= 0) {
$term2 = $prefix[$prev_index];
}
$val = ($term1 - $term2) % $MOD;
if ($val < 0) $val += $MOD;
$new_dp[$t] = $val;
}
}
$dp = $new_dp;
$min_index++;
}
$F = 0;
for ($i = 0; $i < $k; $i++) {
$F = ($F + $dp[$i]) % $MOD;
}
$ans = ($total_ways - $F) % $MOD;
if ($ans < 0) $ans += $MOD;
return $ans;
}
/**
* @param $word
* @return array
*/
function getRuns($word) {
$runs = array();
$n = strlen($word);
$i = 0;
while ($i < $n) {
$j = $i;
while ($j < $n && $word[$j] == $word[$i]) {
$j++;
}
$len = $j - $i;
$runs[] = $len;
$i = $j;
}
return $runs;
}
// Test cases
echo possibleStringCount("aabbccdd", 7) . "\n"; // Output: 5
echo possibleStringCount("aabbccdd", 8) . "\n"; // Output: 1
echo possibleStringCount("aaabbb", 3) . "\n"; // Output: 8
?> Explanation:
This approach efficiently handles the constraints by leveraging dynamic programming and prefix sums to count valid configurations, ensuring optimal performance even for large inputs. |
Beta Was this translation helpful? Give feedback.
We need to determine the number of possible original strings Alice might have intended to type, given the final output string displayed on her screen and a positive integer
k
. The original string must be of length at leastk
. Alice may have pressed a key for too long, resulting in multiple consecutive characters in the output. The solution involves breaking down the problem into manageable parts and using dynamic programming to count valid configurations efficiently.Approach
Problem Analysis: