Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
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 <= 104s and t consist only of lowercase English letters.s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you are trying to find a hidden secret word inside a much larger text by crossing out letters you do not need. The brute force approach tries every possible way to match up pieces of the smaller word against the larger text to see if it can be built correctly.
Here's how the algorithm would work step-by-step:
def is_subsequence_brute_force(target_word, source_text):
# We explore all potential starting positions in the text to ensure we do not miss any valid match
for start_position in range(len(source_text) - len(target_word) + 1):
text_pointer = start_position
target_pointer = 0
# We iterate through the text to match each character of the target word sequentially
while text_pointer < len(source_text) and target_pointer < len(target_word):
if source_text[text_pointer] == target_word[target_pointer]:
target_pointer += 1
text_pointer += 1
# If all characters matched in order, we have successfully verified the subsequence
if target_pointer == len(target_word):
return True
return FalseTo check if a shorter word appears inside a longer sentence in the exact same order, the best strategy is to walk through the sentence from beginning to end while ticking off letters of the shorter word as you find them. This works because we only care about the relative order of the letters, allowing us to scan the sentence just once.
Here's how the algorithm would work step-by-step:
class Solution:
def isSubsequence(self, short_word: str, long_sentence: str) -> bool:
pointer_short = 0
pointer_long = 0
# We iterate through the long sentence to check characters in order.
while pointer_short < len(short_word) and pointer_long < len(long_sentence):
if short_word[pointer_short] == long_sentence[pointer_long]:
pointer_short += 1
pointer_long += 1
# If we reached the end of the short word, all characters were found.
return pointer_short == len(short_word)| Case | How to Handle |
|---|---|
| String s is empty | An empty string is trivially a subsequence of any string t, so the two pointer approach will immediately return true. |
| String t is empty while s is not empty | Since t has no characters to match s, the pointers will exhaust t and correctly return false. |
| Both strings s and t are empty | An empty s is a subsequence of an empty t, resulting in an immediate true return value. |
| String s is longer than string t | A longer string cannot be a subsequence of a shorter one, and the pointer logic will finish without matching all of s, returning false. |
| Strings contain identical characters or are identical | The two pointers will advance in lockstep through both strings until s is fully matched, returning true. |
| String t contains many interleaving or distractor characters | The algorithm simply skips over characters in t that do not match the current character of s until a match is found or t ends. |
| Very large strings s and t up to constraints | The linear time complexity O(n) and constant space complexity O(1) two pointer approach easily handles maximum constraints without stack overflow or memory limits. |
| Unicode characters or multi-byte strings | Most language runtimes handle character iteration properly, but care must be taken with code points versus code units if indices cross multi-byte boundaries. |