2014. Longest Subsequence Repeated k Times #1858
-
Topics: You are given a string A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. A subsequence
Return the longest subsequence repeated Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to find the longest subsequence repeated Approach
Let's implement this solution in PHP: 2014. Longest Subsequence Repeated k Times <?php
/**
* @param String $s
* @param Integer $k
* @return String
*/
function longestSubsequenceRepeatedK($s, $k) {
$n = strlen($s);
$freq = array_fill(0, 26, 0);
for ($i = 0; $i < $n; $i++) {
$char = $s[$i];
$idx = ord($char) - ord('a');
$freq[$idx]++;
}
$valid = [];
for ($i = 0; $i < 26; $i++) {
if ($freq[$i] >= $k) {
$char = chr(ord('a') + $i);
$valid[] = $char;
}
}
if (empty($valid)) {
return "";
}
rsort($valid);
$max_len = min(7, intdiv($n, $k));
for ($len = $max_len; $len >= 1; $len--) {
$result = dfs($len, "", $valid, $freq, $k, $s);
if ($result !== "") {
return $result;
}
}
return "";
}
/**
* @param $len
* @param $path
* @param $valid
* @param $freq
* @param $k
* @param $s
* @return array|bool|int|int[]|mixed|string|null
*/
private function dfs($len, $path, $valid, $freq, $k, $s) {
if ($len == 0) {
if (isKSub($s, $path, $k)) {
return $path;
}
return "";
}
foreach ($valid as $c) {
$count_c = 0;
$path_len = strlen($path);
for ($i = 0; $i < $path_len; $i++) {
if ($path[$i] == $c) {
$count_c++;
}
}
$idx = ord($c) - ord('a');
$max_use = intdiv($freq[$idx], $k);
if ($count_c < $max_use) {
$new_path = $path . $c;
$res = dfs($len - 1, $new_path, $valid, $freq, $k, $s);
if ($res !== "") {
return $res;
}
}
}
return "";
}
/**
* @param $s
* @param $seq
* @param $k
* @return bool
*/
private function isKSub($s, $seq, $k) {
$seq_len = strlen($seq);
if ($seq_len == 0) {
return true;
}
$j = 0;
$rep = 0;
$s_len = strlen($s);
for ($i = 0; $i < $s_len; $i++) {
if ($s[$i] == $seq[$j]) {
$j++;
if ($j == $seq_len) {
$rep++;
if ($rep == $k) {
return true;
}
$j = 0;
}
}
}
return false;
}
// Test cases
echo longestSubsequenceRepeatedK("letsleetcode", 2); // Output: "let"
echo longestSubsequenceRepeatedK("bb", 2); // Output: "b"
echo longestSubsequenceRepeatedK("ab", 2); // Output: ""
?> Explanation:
This approach efficiently narrows down potential candidates while ensuring optimality by leveraging sorting and early termination strategies. |
Beta Was this translation helpful? Give feedback.
We need to find the longest subsequence repeated
k
times in a given strings
. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. The solution involves generating potential subsequences in descending lexicographical order and checking if they can be repeatedk
times withins
.Approach
k
times can be part of the solution.