↓Skip to main content
  1. LeetCode/

LeetCode 392: Is Subsequence

·2 mins· ·
LeetCode Easy Two-Pointers String Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-23

  • Submissions: Runtime Error ×2 → Accepted ×2 → Runtime Error → Accepted ×2 → Runtime Error → Wrong Answer → Accepted ×3 → Runtime Error → Wrong Answer → Runtime Error ×2 → Accepted

Approach
#

One pointer per string (Two Pointers Template 2): j scans t, and i advances when t[j] == s[i]. Loop while i < len(s) and j < len(t), and succeed if i == len(s). Empty s or t then needs no special case. For the follow-up with many s, store the positions of each character of t and binary search for the next one per s.

My Attempts
#

I wrote this many times (2022, 2025, 2026), and almost every error was about empty strings and bounds:

  • 2022-08 used a queue with pop(0); with an empty s, queue[0] was out of range.
  • 2022-11 wrote if not t or not s: return False, but an empty s should be True: ('', '') returned False.
  • In 2025 the inner while t[r] != s[l] raised IndexError for an empty t; adding if not t: return False broke ('', '') again, and r += 1 after a match was never bound-checked: ('bb', 'b') raised IndexError.
  • The 2026 version checked right == m only on a mismatch, so an empty t overran on the first comparison; putting both bounds in the while condition passed.

Solution
#

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        i = 0
        for c in t:
            if i < len(s) and s[i] == c:
                i += 1
        return i == len(s)

Complexity
#

Time O(n + m), space O(1).

Mistakes to Avoid
#

  • An empty s is always a subsequence, even of an empty t.
  • Bound-check j before reading t[j], including right after a match advances it.

Interview Explanation
#

With two pointers I scan t and advance s’s pointer on each match; s is a subsequence if its pointer reaches the end. Time O(n + m), space O(1).

Related

LeetCode 344: Reverse String
·1 min
LeetCode Easy Two-Pointers String
LeetCode 344 note: swap with two pointers and move inward to reverse in place
LeetCode 125: Valid Palindrome
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 125 note: two pointers skip non-alphanumeric characters and compare lowercase letters, with digits included
LeetCode 28: Find the Index of the First Occurrence in a String
·1 min
LeetCode Easy Two-Pointers String String-Matching Z-Algorithm
LeetCode 28 note: try matching needle at every start, or use str.find; index raises when not found
LeetCode 345: Reverse Vowels of a String
·1 min
LeetCode Easy Two-Pointers String
LeetCode 345 note: the same two pointers as LC 917, with the condition switched to “is a vowel” (both cases)
LeetCode 283: Move Zeroes
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 283 note: the write pointer advances only on non-zero values, swapping with them, which pushes zeros to the back
LeetCode 290: Word Pattern
·1 min
LeetCode Easy Hash-Table String
LeetCode 290 note: pattern characters and words must correspond one to one; use two dicts or compare set sizes