Is Subsequence
Problem Statement
Given two strings s and t, determine if s is a subsequence of t. Return true if it is, otherwise return false.
A subsequence of a string is a new string formed from the original string by deleting some (possibly none) characters without changing the order of the remaining characters. For example, "ace" is a subsequence of "abcde", but "aec" is not.
Examples
Example 1:
Input: s = "abc", t = "ahbgdc"
Output: true
Example 2:
Input: s = "axc", t = "ahbgdc"
Output: false
Constraints
0 <= s.length <= 1000 <= t.length <= 10^4sandtconsist only of lowercase English letters.
Follow Up
Suppose there are a large number of incoming strings s1, s2, ..., sk where k >= 10^9, and you want to check for each if t contains it as a subsequence. How would you optimize your approach for this scenario?
Code Template
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
# Your code here
pass
Solutions
- Solution 1: This approach uses two pointers to check if
sis a subsequence oft. It has a time complexity of O(n) and a space complexity of O(1). - Solution 2: This approach preprocesses
tto allow for efficient checking of multiplesstrings using binary search. It has a preprocessing time complexity of O(n) and each query takes O(m log n), where m is the length ofsand n is the length oft. The space complexity is O(n).
| Back to Problem List | Back to Categories |