Is Subsequence
Description
Given a string s and a string t, check if s is subsequence of t.
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and sis a short string (<=100).
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace"
is a subsequence of "abcde"
while "aec"
is not).
Example 1:
Example 2:
Follow up: If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
Solutions
For original
For follow-up
Store the indices of characters in t
in a hash table T
.
For each s
, use the following algorithm check is s
is a subsequence of t
.
Let i
be the index of the first unused character in t
. Initially i = 0
.
For each character c
in s
, find the first index of c
in t[i..]
. The indices of c
are stored in T[c]
as a sorted list. We can use binary search to find the index in O(t.size())
time. If no such index, return false. Otherwise set i
to the index + 1.
Let n
be the average length of s
, k
be the number of s
, m
be the length of t
. Then the running time is O(m + k * n * log(m))
. running time using the solution for the original problem is O(k * m)
.
Last updated