Longest Subsequence Repeated k Times - Problem

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 repeated 2 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.

Input & Output

Example 1 — Basic Case
$ Input: s = "letsleetcode", k = 2
Output: "let"
💡 Note: The subsequence "let" can be repeated 2 times to form "letlet", which is a subsequence of "letsleetcode". This is the longest such subsequence.
Example 2 — No Valid Subsequence
$ Input: s = "bb", k = 3
Output: ""
💡 Note: No character appears 3 or more times, so no subsequence can be repeated 3 times.
Example 3 — Single Character
$ Input: s = "ab", k = 1
Output: "b"
💡 Note: When k=1, we want the lexicographically largest subsequence, which is "b".

Constraints

  • 2 ≤ s.length ≤ 2000
  • 2 ≤ k ≤ 2000
  • s consists of lowercase English letters

Visualization

Tap to expand
Longest Subsequence Repeated k TimesOriginal: "bababcba", k = 2Subsequence: "bba"Repeated: "bba" × 2 = "bbabba"Valid? ✓b_a_b_a_b_c_b_ab-b-a-------b-a↑ Forms "bbabba"Result: "bba" (length 3)
Understanding the Visualization
1
Input
String s and repetition count k
2
Process
Find subsequence that when repeated k times is still a subsequence
3
Output
Longest valid subsequence (lexicographically largest if tie)
Key Takeaway
🎯 Key Insight: Only characters appearing ≥ k times can be part of a valid subsequence
Asked in
Google 25 Microsoft 18 Amazon 15 Meta 12
12.5K Views
Medium Frequency
~35 min Avg. Time
487 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen