Find the longest subsequence that can be repeated times to form a subsequence of the given string.
LeetCode 2014
gfgYou are given a string and a positive integer .
A string is called good if repeating it exactly times gives a string that is a subsequence of .
Return the longest good string . If there are multiple answers with the same maximum length, return the lexicographically largest one.
A subsequence is formed by deleting zero or more characters without changing the order of the remaining characters.
Input Format
- A string
- An integer
The exact platform input format may vary; the problem is logically defined by these two values.
Output Format
Return the longest string such that is a subsequence of . If multiple strings have the same length, return the lexicographically largest one.
Constraints
- Characters are lowercase English letters
- The answer can be empty if no non-empty good string exists
Example 1
Input
s = "letsleetcode", k = 2
Output
"let"
Explanation
The string "letlet" is a subsequence of "letsleetcode", so "let" is good. No longer good string exists.
Example 2
Input
s = "bb", k = 2
Output
"b"
Explanation
Repeating "b" twice gives "bb", which is a subsequence of the input.
Show 1 more example
Example 3
Input
s = "ab", k = 2
Output
""
Explanation
No non-empty string repeated twice can be formed as a subsequence of "ab".
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.