Problem 307568 · easy · Level 03 Linear Management & Searching

Longest Prefix That Fits as a Subsequence

two pointers · strings · subsequence

Given two strings s and t, return the length of the longest prefix of s that is a subsequence of t (characters of t may be skipped but not reordered).

Examples

Input:  s = "abc", t = "acb"
Output: 2
Explanation: "ab" is a subsequence of "acb" but "abc" is not.

Input:  s = "axc", t = "abcxc"
Output: 3

Constraints

  • 0 <= len(s), len(t) <= 10**5
  • Target: O(len(t)) time, O(1) extra space.

Goals

  • Advance one pointer only on a match and the other unconditionally
  • Read the answer off the pointer position at the end
Starting Python…