Is Subsequence - Problem

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. For example, "ace" is a subsequence of "abcde" while "aec" is not.

Input & Output

Example 1 — Basic Subsequence
$ Input: s = "abc", t = "aabbcc"
Output: true
💡 Note: We can find 'a', then 'b', then 'c' in order within "aabbcc": a(a)b(b)c(c)
Example 2 — Not a Subsequence
$ Input: s = "axc", t = "ahbgdc"
Output: false
💡 Note: We find 'a', then find 'h', 'b', 'g', 'd' but no 'x' appears before 'c'
Example 3 — Empty Subsequence
$ Input: s = "", t = "hello"
Output: true
💡 Note: Empty string is always a subsequence of any string

Constraints

  • 0 ≤ s.length ≤ 100
  • 0 ≤ t.length ≤ 104
  • s and t consist only of lowercase English letters

Visualization

Tap to expand
Is Subsequence: Check if s is subsequence of ts = "abc"t = "aabbcc"Find characters of s in order within taabbccGreen = used, Gray = skippedResult: true
Understanding the Visualization
1
Input
Two strings s and t
2
Process
Check if s can be formed by deleting characters from t
3
Output
Return true if s is a subsequence of t
Key Takeaway
🎯 Key Insight: Use two pointers to scan once - no need to backtrack or try all combinations
Asked in
Amazon 8 Google 5 Microsoft 4 Facebook 3
285.0K Views
Medium Frequency
~15 min Avg. Time
4.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen