2014. Longest Subsequence Repeated k Times
Description
You are given a string s
of length n
, and an integer k
. You are tasked to find the longest subsequence repeated k
times in string s
.
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 seq
is repeated k
times in the string s
if seq * k
is a subsequence of s
, where seq * k
represents a string constructed by concatenating seq
k
times.
- For example,
"bba"
is repeated2
times in the string"bababcba"
, because the string"bbabba"
, constructed by concatenating"bba"
2
times, is a subsequence of the string"bababcba"
.
Return the longest subsequence repeated k
times in string s
. If multiple such subsequences are found, return the lexicographically largest one. If there is no such subsequence, return an empty string.
Example 1:
Input: s = "letsleetcode", k = 2 Output: "let" Explanation: There are two longest subsequences repeated 2 times: "let" and "ete". "let" is the lexicographically largest one.
Example 2:
Input: s = "bb", k = 2 Output: "b" Explanation: The longest subsequence repeated 2 times is "b".
Example 3:
Input: s = "ab", k = 2 Output: "" Explanation: There is no subsequence repeated 2 times. Empty string is returned.
Constraints:
n == s.length
2 <= k <= 2000
2 <= n < min(2001, k * 8)
s
consists of lowercase English letters.
Solutions
Solution 1: BFS
We can first count the occurrences of each character in the string, and then store the characters that appear at least \(k\) times in a list \(\textit{cs}\) in ascending order. Next, we can use BFS to enumerate all possible subsequences.
We define a queue \(\textit{q}\), initially putting the empty string into the queue. Then, we take out a string \(\textit{cur}\) from the queue and try to append each character \(c \in \textit{cs}\) to the end of \(\textit{cur}\) to form a new string \(\textit{nxt}\). If \(\textit{nxt}\) is a subsequence that can be repeated \(k\) times, we add it to the answer and put \(\textit{nxt}\) back into the queue for further processing.
We need an auxiliary function \(\textit{check(t, k)}\) to determine whether the string \(\textit{t}\) is a repeated \(k\) times subsequence of string \(s\). Specifically, we can use two pointers to traverse \(s\) and \(\textit{t}\). If we can find all characters of \(\textit{t}\) in \(s\) and repeat this process \(k\) times, then return \(\textit{true}\); otherwise, return \(\textit{false}\).
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|