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)

๐Ÿ› ๏ธ 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

๐Ÿ“š Reference Solution

โ–ผ
โŒ˜K Search โŒ˜โ†ฉ Run โŒ˜S Submit