Is Subsequence - Complete Solution Guide
Is Subsequence is LeetCode problem 392, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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. (i.e., "ace" is a subsequence of " a b c d e " while "aec" is not). Example 1: Input: s = "abc", t = "ahbgdc" Output: true Example 2: Input: s = "axc", t = "ahbgdc" Output: false Constraints: 0 <= s.length <=
Detailed Explanation
The problem asks us to determine if a string `s` is a subsequence of another string `t`. A subsequence is formed by deleting zero or more characters from the original string without changing the order of the remaining characters. For instance, "ace" is a subsequence of "abcde", but "aec" is not because the characters are not in the correct order.
Solution Approach
The provided solution uses a two-pointer approach. One pointer `i` tracks the index in string `s` and the other pointer `j` tracks the index in string `t`. The algorithm iterates through both strings. If `s[i]` matches `t[j]`, we increment `i` to check for the next character in `s`. Regardless of a match, we always increment `j` to move through `t`. The algorithm returns `true` if `i` reaches the end of `s`, indicating that all characters in `s` were found in `t` in the correct order, otherwise, it returns `false`.
Step-by-Step Algorithm
- Step 1: Initialize two pointers, `i` to 0 (index for string `s`) and `j` to 0 (index for string `t`).
- Step 2: Iterate through both strings using a `while` loop, continuing as long as `i` is within the bounds of `s` and `j` is within the bounds of `t`.
- Step 3: Inside the loop, compare `s[i]` and `t[j]`. If they are equal, it means we have found the next character of the subsequence `s` in `t`. Increment `i`.
- Step 4: Regardless of whether the characters match, increment `j` to move to the next character in `t`.
- Step 5: After the loop finishes (either `i` reaches the end of `s` or `j` reaches the end of `t`), check if `i` is equal to the length of `s`. If it is, it means we have found all characters of `s` in `t` in the correct order, so return `true`. Otherwise, return `false`.
Key Insights
- Insight 1: The order of characters in `s` must be preserved in `t`. We can use two pointers to iterate through `s` and `t` simultaneously.
- Insight 2: We only need to increment the pointer for `s` when we find a matching character in `t`. The pointer for `t` always increments to scan all of its characters.
- Insight 3: If we successfully iterate through all characters of `s`, then `s` is a subsequence of `t`. If we reach the end of `t` before finishing `s`, then `s` is not a subsequence of `t`.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Two Pointers, String, Dynamic Programming.
Companies
Asked at: Accenture, Electronic Arts, Nvidia, Pinterest, Qualcomm, Salesforce, Tekion, Tesla, Tinkoff, Wix, Yandex.