392. Is Subsequence - Detailed Explanation
Problem Statement
Description:
Given two strings, s and t, determine whether s is a subsequence of t. A subsequence of a string is formed by deleting some (or no) characters from the original string without changing the relative order of the remaining characters.
Definition:
- Subsequence: A string sis a subsequence oftif you can remove zero or more characters fromtto getswhile preserving the order of the characters.
Examples:
- 
Example 1: - Input: s = "abc",t = "ahbgdc"
- Output: true
- Explanation:
 The characters"a","b","c"appear in"ahbgdc"in the same order (i.e."a", then"b", then"c").
 
- Input: 
- 
Example 2: - Input: s = "axc",t = "ahbgdc"
- Output: false
- Explanation:
 Although"a"and"c"appear in"ahbgdc", the letter"x"does not appear in the correct order to form"axc".
 
- Input: 
Constraints:
- 0 ≤ s.length≤ 100
- 0 ≤ t.length≤ 10⁴
- Both sandtconsist of lowercase English letters.
Hints
- 
Hint 1: 
 Use two pointers—one forsand one fort. Traversetand check if the current character matches the current character ins. If it does, move the pointer ins.
- 
Hint 2: 
 Once you reach the end ofs, you know that all characters ofshave been found in order int.
- 
Hint 3: 
 If you finish traversingtand haven’t advanced the pointer insto the end, thensis not a subsequence oft.
Approaches
Two-Pointer Approach (Optimal)
Idea:
- Use one pointer (i) to iterate oversand another pointer (j) to iterate overt.
- For each character in t, check if it matches the current character ins(i.e.s[i]).
- If they match, move the pointer in sforward.
- Continue until either pointer reaches the end of its respective string.
- If the pointer for sreaches the end (i.e.i == s.length), thensis a subsequence oft.
Steps:
- Initialize two pointers: i = 0forsandj = 0fort.
- While both i < s.lengthandj < t.length:- If s[i] == t[j], incrementi(found a matching character).
- Always increment jto move to the next character int.
 
- If 
- After the loop, check if iis equal tos.length. If yes, returntrue; otherwise, returnfalse.
3.2. Dynamic Programming / Binary Search (For Multiple Queries)
Idea:
- Preprocess tto record the indices of each character.
- For each query string s, use binary search to find whether each character insappears intafter the previous character’s index.
- This method is useful if you have multiple queries against the same string t.
Note:
For a single query, the two-pointer approach is more straightforward and efficient.
Detailed Walkthrough (Two-Pointer Approach)
Let’s consider the example where s = "abc" and t = "ahbgdc".
- 
Initialization: - i = 0(points to- 'a'in- s)
- j = 0(points to- 'a'in- t)
 
- 
Iteration: - Step 1: Compare s[0]('a') witht[0]('a'):- They match → increment ito 1.
- Increment jto 1.
 
- They match → increment 
- Step 2: Now, compare s[1]('b') witht[1]('h'):- No match → keep iat 1, incrementjto 2.
 
- No match → keep 
- Step 3: Compare s[1]('b') witht[2]('b'):- They match → increment ito 2, incrementjto 3.
 
- They match → increment 
- Step 4: Compare s[2]('c') witht[3]('g'):- No match → keep iat 2, incrementjto 4.
 
- No match → keep 
- Step 5: Compare s[2]('c') witht[4]('d'):- No match → keep iat 2, incrementjto 5.
 
- No match → keep 
- Step 6: Compare s[2]('c') witht[5]('c'):- They match → increment ito 3, incrementjto 6.
 
- They match → increment 
 
- Step 1: Compare 
- 
Conclusion: - Now i == s.length(3 == 3), so all characters ofswere found intin order.
- Return true.
 
- Now 
Code Implementation
Python Code
Java Code
Complexity Analysis
- 
Time Complexity: 
 O(n + m), where n is the length of stringsand m is the length of stringt.
 In the worst case, every character oftis checked, and each character ofsis compared at most once.
- 
Space Complexity: 
 O(1), since we use only two integer pointers regardless of the input size.
Additional Sections
Common Mistakes
- 
Not Advancing the Pointer in t:
 Failing to increment thejpointer inton every iteration can lead to an infinite loop.
- 
Incorrect Edge Case Handling: 
 Not checking cases wheresis empty (which should returntrue) or whentis empty (except whensis also empty).
Edge Cases
- 
Empty s:
 An empty string is considered a subsequence of any string.
- 
Empty t:
 Iftis empty andsis not, thenscannot be a subsequence oft.
- 
Identical Strings: 
 Ifsequalst, the answer istrue.
Alternative Variations and Related Problems
- 
Variations: - Longest Common Subsequence (LCS): A related problem where the goal is to find the longest subsequence common to two strings.
- Subsequence Counting: Count the number of times a subsequence appears in a given string.
 
- 
Related Problems for Further Practice: - 
Longest Increasing Subsequence: A dynamic programming problem on sequences. 
- 
Edit Distance: Another string manipulation problem that involves comparing sequences. 
 
- 
GET YOUR FREE
Coding Questions Catalog
$197

$78
$78