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 none, of the characters from the original string without changing the relative order of the remaining characters.

Definition:

  • Subsequence: A string s is a subsequence of t if you can remove zero or more characters from t to get s while preserving the order of the characters.

This question is the plainest instance of the Two Pointers pattern, the technique that moves two indices through the input at different speeds instead of nesting one loop inside another. Here one index advances only on a match, the other advances every step. That pairing shows up repeatedly, as the summary of the most common LeetCode patterns shows.

Examples:

  • Example 1:

    • Input: s = "abc", t = "ahbgdc"
    • Output: true
    • Explanation:
      The characters "a", "b", "c" appear in "ahbgdc" in the same order, that is "a", then "b", then "c".
  • 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".

Constraints:

  • 0 ≤ s.length ≤ 100
  • 0 ≤ t.length ≤ 10⁴
  • Both s and t consist of lowercase English letters.

Hints

  • Hint 1:
    Use two pointers, one for s and one for t. Traverse t and check whether the current character matches the current character in s. If it does, move the pointer in s.

  • Hint 2:
    Once you reach the end of s, you know that all characters of s have been found in order in t.

  • Hint 3:
    If you finish traversing t without advancing the pointer in s to the end, then s is not a subsequence of t.

Approaches

Two pointer approach (optimal)

Idea:

  • Use one pointer (i) to iterate over s and another pointer (j) to iterate over t.
  • For each character in t, check whether it matches the current character in s, meaning s[i].
  • If they match, move the pointer in s forward.
  • Continue until either pointer reaches the end of its own string.
  • If the pointer for s reaches the end, meaning i == s.length, then s is a subsequence of t.

Steps:

  1. Initialize two pointers: i = 0 for s and j = 0 for t.
  2. While both i < s.length and j < t.length:
    • If s[i] == t[j], increment i, since you found a matching character.
    • Always increment j to move to the next character in t.
  3. After the loop, check whether i equals s.length. If it does, return true. Otherwise, return false.

Dynamic programming or binary search (for multiple queries)

Idea:

  • Preprocess t to record the indices of each character.
  • For each query string s, use binary search to find whether each character in s appears in t after the index of the previous character.
  • This method helps when you run many queries against the same string t.

Note:
For a single query, the two pointer approach is simpler and just as fast.

Detailed walkthrough (two pointer approach)

Consider the example where s = "abc" and t = "ahbgdc".

  1. Initialization:

    • i = 0 (points to 'a' in s)
    • j = 0 (points to 'a' in t)
  2. Iteration:

    • Step 1: Compare s[0] ('a') with t[0] ('a'):
      • They match → increment i to 1.
      • Increment j to 1.
    • Step 2: Now compare s[1] ('b') with t[1] ('h'):
      • No match → keep i at 1, increment j to 2.
    • Step 3: Compare s[1] ('b') with t[2] ('b'):
      • They match → increment i to 2, increment j to 3.
    • Step 4: Compare s[2] ('c') with t[3] ('g'):
      • No match → keep i at 2, increment j to 4.
    • Step 5: Compare s[2] ('c') with t[4] ('d'):
      • No match → keep i at 2, increment j to 5.
    • Step 6: Compare s[2] ('c') with t[5] ('c'):
      • They match → increment i to 3, increment j to 6.
  3. Conclusion:

    • Now i == s.length (3 == 3), so all characters of s were found in t in order.
    • Return true.

Code implementation

Python code

Python3
Python3

. . . .

Java code

Java
Java

. . . .

Complexity analysis

  • Time Complexity:
    O(n + m), where n is the length of string s and m is the length of string t.
    In the worst case, every character of t is checked, and each character of s is compared at most once.

  • Space Complexity:
    O(1), since we use only two integer pointers regardless of the input size.

The same scan, written out line by line, is taught in the Is Subsequence lesson in Grokking the Google Coding Interview.

Additional notes

Common mistakes

  • Not advancing the pointer in t:
    Failing to increment the j pointer in t on every iteration can produce an infinite loop.

  • Incorrect edge case handling:
    Skipping the case where s is empty, which should return true, or the case where t is empty, which returns true only when s is empty as well.

Edge cases

  • Empty s:
    An empty string counts as a subsequence of any string.

  • Empty t:
    If t is empty and s is not, then s cannot be a subsequence of t.

  • Identical strings:
    If s equals t, the answer is true.

  • Variations:

    • Longest Common Subsequence (LCS): A related question that asks for the longest subsequence common to two strings.
    • Subsequence Counting: Count the number of times a subsequence appears in a given string.
  • More two pointer practice:

    • Two indices that walk toward each other drive Container With Most Water, where the shorter side moves inward each step.
    • A write pointer and a read pointer over one array drive Move Zeroes, which is the closest relative of this question.
  • Related problems for further practice:

TAGS
Coding Interview Questions
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Is Grokking the Coding Interview Available as a Book or PDF?
Grokking the Coding Interview is delivered as an online course and Design Gurus sells no PDF of it; here is what the course contains, why GitHub copies fall short, and what is free.
Will AI get rid of coding jobs?
Who is Palantir's biggest competitor?
What to expect in a Meta (Facebook) technical interview?
How do I introduce myself in Google interview?
What Is the Snap Interview Process Like? (Round by Round)
The Snap software engineer interview: recruiter screen, a one-hour coding screen, and a virtual onsite of four to five interviews, in three to six weeks.
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$99

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.