Ctrl K

Removals That Keep the Pattern

Medium
You are given strings text and pattern, where pattern is a subsequence of text: it can be obtained by crossing out some characters of text without reordering the rest. You also get an array order of distinct indices into text (0-based). Characters are crossed out one by one in the order of the array: first text[order[0]], then text[order[1]], and so on. The indices always refer to the original string. Return the largest k (from 0 to len(order)) such that after crossing out the first k characters listed in order, pattern is still a subsequence.
Example 1
text='banana', pattern='ban', order=[5, 1, 0, 3] → 2

After removing positions 5 and 1 we are left with "bnan", which still has b, a, n in order. The third removal takes away the only "b".

Example 2
text='abcab', pattern='ab', order=[0, 1, 2] → 3

Even after all three removals, "ab" is left.

Example 3
text='xyz', pattern='xz', order=[2, 1] → 0

The very first removal deletes "z", so nothing can be removed.

Constraints
  • 1 ≤ len(pattern) ≤ len(text) ≤ 10⁵
  • 0 ≤ len(order) ≤ len(text)
  • 0 ≤ order[i] < len(text), and all order[i] are distinct
  • text and pattern consist of lowercase English letters
  • pattern is a subsequence of text
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair in a Sorted List MediumTriples Under a Limit MediumSpot the Extra Copy MediumWarm Every Cabin MediumA Subsequence of One Word Only Medium
loading Python…
Input
text='banana', pattern='ban', order=[5, 1, 0, 3]
Expected
2

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