Longest Subsequence Repeated k Times Problem
Description
LeetCode Problem 2014.
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 repeated2times in the string"bababcba", because the string"bbabba", constructed by concatenating"bba"2times, is a subsequence of the string"**b**a**bab**c**ba**".
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.length2 <= k <= 20002 <= n < min(2001, k * 8)sconsists of lowercase English letters.
Solution
Approach: Breadth-First Search Over Candidates
Because n < 8k, the answer can have at most 7 characters, and each of its letters must occur at least k times in s, since the answer is repeated k times. That leaves only a few candidate letters.
Build candidates level by level, starting from the empty string. For every candidate, try appending each allowed letter in alphabetical order, and keep the extension if it can be repeated k times as a subsequence of s. This check is monotone: if a string passes, so does every prefix of it, so no valid candidate is missed. A greedy scan checks a candidate t: walk through s, match t character by character, and count a copy each time the end of t is reached.
Candidates in a level come out in alphabetical order, so the last one that passes is the lexicographically largest of the longest length. Return the last successful string seen.
Complexity: O(|Σ'|7 * n) in the worst case, where Σ' is the set of letters that occur at least k times (at most n / k < 8 of them), so it is small in practice; the space is for the queue of candidates.
from collections import Counter, deque
from string import ascii_lowercase
class Solution:
def longestSubsequenceRepeatedK(self, s: str, k: int) -> str:
def check(t: str, k: int) -> bool:
i = 0
for c in s:
if c == t[i]:
i += 1
if i == len(t):
k -= 1
if k == 0:
return True
i = 0
return False
cnt = Counter(s)
cs = [c for c in ascii_lowercase if cnt[c] >= k]
q = deque([""])
ans = ""
while q:
cur = q.popleft()
for c in cs:
nxt = cur + c
if check(nxt, k):
ans = nxt
q.append(nxt)
return ans