MEDIUM Two Pointers
392. 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.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
- โขIn-place array updates
- โขSorted array traversal
- โขBoundary condition checks
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
- โขApply Two Pointers reasoning pattern
๐ก Approach
- โ Use two pointers, one for each string
- โ Move pointer in t when match found
- โ If s pointer reaches end, check if t pointer also at end
- โ Time: O(n + m), Space: O(1)
๐งญ Prerequisites
๐ ๏ธ Hints & Pitfalls
Hints
- โขUse two pointers, one for each string
- โขMove pointer in t when match found
- โขIf s pointer reaches end, check if t pointer also at end
Common Pitfalls
- โขTime: O(n + m), Space: O(1)
๐งช Test Cases
Hidden tests on submit: 1
Test Case 1
Not run Input:
isSubsequence('abc', 'ahbgdc'); Expected:
false Test Case 2
Not run Input:
isSubsequence('axc', 'ahbgdc'); Expected:
false Test Case 3
Not run Input:
isSubsequence('', 'abc'); Expected:
false ๐ Code Editor
๐ค Output