Leetcode 392: Is Subsequence

grid47
grid47
Exploring patterns and algorithms
Sep 28, 2024 5 min read

A sequence of characters gently forming into a subsequence, glowing as each match is found.
Solution to LeetCode 392: Is Subsequence Problem

Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence is formed by deleting some characters of t while maintaining the relative order of the remaining characters.
Problem
Approach
Steps
Complexity
Input: The input consists of two strings, s and t.
Example: s = 'abc', t = 'ahbgdc'
Constraints:
• 0 <= s.length <= 100
• 0 <= t.length <= 10^4
• s and t consist only of lowercase English letters.
Output: The output is a boolean indicating whether s is a subsequence of t.
Example: Output: true
Constraints:
• The output should be true if s is a subsequence of t, otherwise false.
Goal: The goal is to check if string s can be derived from string t by deleting characters while preserving the order of the remaining characters.
Steps:
• Use two pointers to traverse both strings.
• Start with both pointers at the beginning of their respective strings.
• Move through string t and match characters with string s one by one.
• If all characters in s are matched in order, return true, otherwise return false.
Goal: Ensure the solution handles the constraints efficiently.
Steps:
• The solution must handle inputs with lengths up to 10^4 efficiently.
Assumptions:
• Both strings consist only of lowercase English letters.
Input: Input: s = 'abc', t = 'ahbgdc'
Explanation: Starting with pointers at the beginning of both strings, 'a' matches, then 'b' matches, and finally 'c' matches in order, so 'abc' is a subsequence of 'ahbgdc'.

Input: Input: s = 'axc', t = 'ahbgdc'
Explanation: Starting from the first letter of both strings, 'a' matches, but 'x' does not appear in order in 'ahbgdc', so 'axc' is not a subsequence of 'ahbgdc'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus