Skip to main content
Back to problems
Leetcode
Medium
Strings
Recursion
Combinatorics
Google
Longest Subsequence Repeated K Times

Find the longest subsequence that can be repeated kk times to form a subsequence of the given string.

Acceptance 100%
Also Available On
Other platform versions and source mappings for the same problem.

LeetCode 2014

gfg
Problem Statement

You are given a string ss and a positive integer kk.

A string tt is called good if repeating it exactly kk times gives a string tkt^k that is a subsequence of ss.

Return the longest good string tt. 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 ss
  • An integer kk

The exact platform input format may vary; the problem is logically defined by these two values.

Output Format

Return the longest string tt such that tkt^k is a subsequence of ss. If multiple strings have the same length, return the lexicographically largest one.

Constraints

  • 1s1 \le |s|
  • 1k1 \le k
  • Characters are lowercase English letters
  • The answer can be empty if no non-empty good string exists
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.