392. Is Subsequence πŸ“

Difficulty: Easy - Tags: Two Pointers, String

LeetCode Problem Link

Problem Statement πŸ“œ

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.

Examples 🌟

πŸ”Ή Example 1:

Input:

s = "abc"
t = "ahbgdc"

Output:

true

πŸ”Ή Example 2:

Input:

s = "axc"
t = "ahbgdc"

Output:

false

Constraints βš™οΈ

  • 0 <= s.length <= 100

  • 0 <= t.length <= 10^4

  • s and t consist only of lowercase English letters.

Solution πŸ’‘

Approach 1: Two-Pointer Technique

  1. Initialize Two Pointers: Use one pointer for each string (i for s and j for t).

  2. Traverse through t: Move through the characters of t. If a character in t matches the current character in s, move the pointer in s to the next position.

  3. End Condition: If the pointer for s reaches the end of s, all characters in s have been matched in order, so return true. Otherwise, return false.

Java Solution

public class Solution {
    public boolean isSubsequence(String s, String t) {
        int i = 0, j = 0;
        while (i < s.length() && j < t.length()) {
            if (s.charAt(i) == t.charAt(j)) {
                i++;
            }
            j++;
        }
        return i == s.length();
    }
}

Time Complexity ⏳

  • O(n + m), where n is the length of s and m is the length of t. We traverse both strings once.

Space Complexity πŸ’Ύ

  • O(1), as no additional space is used.

Follow-up: Efficient Solution for Multiple Queries

If there are multiple s strings (e.g., s1, s2, ..., sk), consider Binary Search with Preprocessing:

  1. Preprocess t: Create a map where each character points to a list of indices in t.

  2. Binary Search for Subsequences: For each s, use binary search to find each character’s position in t that comes after the previously matched character’s index.

You can find the full solution here.

Last updated