Ctrl K

Shift Letters to Reveal a Subsequence

Medium
You get two strings text and pattern of lowercase English letters. At most once, you may pick any set of positions in text and replace each chosen letter with the next one in the alphabet, wrapping around: a → b, b → c, …, z → a. Return True if this can make pattern a subsequence of text, and False otherwise. A subsequence is what remains of a string after deleting some characters (possibly none) without reordering the rest.
Example 1
text="kpz", pattern="la" → True

Shift k → l and z → a to get "lpa", which contains "la" as a subsequence.

Example 2
text="mmx", pattern="ny" → True

Shift the first m → n and x → y: "nmy" contains "ny".

Example 3
text="bd", pattern="f" → False

An f can only come from an e or an f, and text has neither.

Constraints
  • 1 ≤ len(text), len(pattern) ≤ 10⁵
  • Both strings contain only lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Mirror Fragment MediumWords in Reverse Order MediumExactly One Edit Apart MediumWhich Release Is Newer MediumReverse the Word Order in Place Medium
loading Python…
Input
text="kpz", pattern="la"
Expected
True

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