Ctrl K

One Extra Letter, More Pairs

Medium
You are given a string text and a two-letter string pair. You must insert exactly one letter, either pair[0] or pair[1], anywhere in text (the very beginning and the very end are allowed too). Return the largest possible number of times pair occurs as a subsequence of the resulting string. Occurrences are told apart by the positions of the chosen letters. A subsequence is obtained by deleting some characters of a string (possibly none) without changing the order of the rest.
Example 1
text="xyzxy", pair="xy" → 5

Right now xy occurs as a subsequence three times. Adding an x at the front gives xxyzxy with five occurrences; adding a y at the end also gives five.

Example 2
text="bab", pair="ba" → 3

Put a b at the front: bbab. Each of the three bs comes before the only a, so the answer is 3.

Example 3
text="ooo", pair="oo" → 6
Constraints
  • 1 ≤ len(text) ≤ 10⁵
  • len(pair) = 2
  • text and pair consist of lowercase Latin letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumStudios for Recording Sessions MediumEvery Letter Once MediumErase Digits for the Smallest Number MediumSmallest Up-Down Sequence Medium
loading Python…
Input
text="xyzxy", pair="xy"
Expected
5

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