Ctrl K

Erasable Marked Letters

Medium
You get a string text, a string pattern that is a subsequence of text, and an increasing list marked of distinct indices into text. An editor lets you erase letters only at marked positions, one at a time, and only as long as pattern is still a subsequence of the text after each erasure. An erased letter leaves a gap: the other letters keep their indices. Return the largest number of letters you can erase. A string p is a subsequence of t if p can be obtained from t by deleting some characters without changing the order of the rest.
Example 1
text='banana', pattern='ban', marked=[1, 2, 3, 5] → 3

Erase positions 1 and 2: "b__ana" still spells "ban" through b, the a at 3 and the n at 4. Position 3 cannot go as well, since the only a left after b would be at 5, with no n after it. Position 5 can always be erased. That makes 3.

Example 2
text='code', pattern='code', marked=[0, 3] → 0

The pattern is the whole text, so not a single letter can go.

Example 3
text='xaxbx', pattern='ab', marked=[0, 2, 4] → 3

Every marked position holds an x the pattern never needs, so all of them can be erased.

Constraints
  • 1 ≤ len(text) ≤ 3000
  • 1 ≤ len(pattern) ≤ len(text)
  • 1 ≤ len(marked) ≤ len(text)
  • marked is strictly increasing, 0 ≤ marked[i] < len(text)
  • text and pattern contain only 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 problemsWord Growth Chain MediumSplit Into Dictionary Words MediumA Subsequence of One Word Only MediumWords Hidden in a Text MediumBest-Scoring Stretch of Text Medium
loading Python…
Input
text='banana', pattern='ban', marked=[1, 2, 3, 5]
Expected
3

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