Ctrl K

Pattern Woven Several Times

Hard
You are given a string text of lowercase English letters and an integer times. A string pattern is *woven in* times times if writing pattern out times times in a row gives a string you can get from text by crossing out some letters (possibly none) while keeping the rest in order. For example, ba is woven into xbyaba twice: keeping baba from the text does it. Find the longest string woven into text times times. If several strings tie for the longest, return the one that comes last alphabetically. If no non-empty string works, return the empty string.
Example 1
text="tomtomtamtam", times=2 → 'tmtm'

Writing tmtm twice gives tmtmtmtm: the first four letters come from tomtom, the next four from tamtam. No pattern longer than four letters fits twice.

Example 2
text="xyyxzx", times=3 → 'x'

Only the letter x occurs three times, so no pattern longer than one letter can fit three times.

Example 3
text="abcd", times=2 → ''

All letters are different, so not even a single letter fits twice and the answer is the empty string.

Constraints
  • 2 ≤ times ≤ 2000
  • 2 ≤ len(text) ≤ 2000
  • len(text) < 8 · times
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsEqual Letters Kept Apart HardZero-Free Number With a Multiple Digit Product HardMake Letter Counts Equal HardNext Time From the Same Digits MediumNo Twin Letters Side by Side Medium
loading Python…
Input
text="tomtomtamtam", times=2
Expected
'tmtm'

Run checks the first 3 tests in your browser. Submit checks all 71 tests on the server, hidden ones included.