Ctrl K

Nearly Matching Picks

Medium
You are given strings text and pattern, with pattern the shorter one. Pick exactly len(pattern) positions of text in increasing order and read the letters at them. The pick is valid if the resulting string equals pattern or differs from it in exactly one letter (you may replace one letter). Return the valid pick, as a list of positions, that is smallest in dictionary order when compared as a list of numbers (first position first, then the second on a tie, and so on). If there is no valid pick, return an empty list. Note that it is the list of positions that must be smallest, not the string of picked letters.
Example 1
text="xcaty", pattern="bat" → [0, 2, 3]

Pick positions 0, 2, 3: the letters "xat". Replacing x with b gives "bat". The set [1, 2, 3] also works, but [0, 2, 3] is smaller.

Example 2
text="cabd", pattern="abd" → [0, 2, 3]

The exact match is at positions [1, 2, 3]. But we can take position 0 and replace c with a, then find "bd" at 2 and 3: [0, 2, 3] is smaller.

Example 3
text="aaaa", pattern="bcd" → []

At least two of the three letters must match, but text has no b, c or d, so the answer is empty.

Constraints
  • 1 ≤ len(pattern) < len(text) ≤ 3·10⁵
  • Both strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMirror Chunk Split HardDisjoint Long Palindromes HardLongest Mirror Fragment MediumCounting Palindromic Pieces of a String MediumBrackets With Wildcards Medium
loading Python…
Input
text="xcaty", pattern="bat"
Expected
[0, 2, 3]

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