Is Subsequence
LeetCode Problem #392 (Easy)
Problem Statement
Given a string s and a string t, check if s is a subsequence of t.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace"
is a subsequence of "abcde"
while "aec"
is not).
Testcases
Prerequisite Knowledge
Subsequence
A subsequence is a sequence which can be derived from another sequence using zero or more elements while maintaining the relative order of the elements in the original sequence.
Example
Let us consider the string "coding"
.
The sequence of elements in this string is as follows:
The characters in a subsequence of this sequence do not have to be located consecutive to one another, but the relative order of the characters must be maintained.
Correct Subsequence: "cng"
Incorrect Subsequence: "dcn"
For a sequence of size n
, we can have a total of 2n - 1
non-empty subsequences.
Dynamic Programming
Refer to the Prerequisite Knowledge section in Coin Change 2:
Algorithm
Two-Pointer Approach
In this approach, we use two pointers to compare the characters in both strings at the same time, while maintaining the relative order of the characters. If the number of same characters is equal to the smaller string, then we can conclude that the smaller string is a subsequence of the larger string.
Iterate through each string.
Check if the characters from each string are equal.
a. If the characters are equal, increment the pointer of the first string.
b. If the characters are not equal, increment the pointer of the second string.
Check if the value of the first pointer is equal to the length of the first string when the loop exits.
a. If the values are equal, return true.
b. If the values are unequal, return false.
Top-Down DP Approach
In this approach, we use a helper function to find the longest common subsequence (LCS) between the two given strings by building a top-down DP table, which returns the length of the LCS. In the function provided in the IDE to check if the smaller string is a subsequence of the larger string, we compare the length of the smaller string with the value returned by the helper function.
Longest Common Subsequence Algorithm
Initialize a two-dimensional array (m x n) to build the DP table.
Build the table by checking the following conditions:
a. If the cell is in the first row or first column (i = 0 or j = 0), then initialize the value of the cell as 0.
b. If the characters being compared in each string are equal, the value of the cell is obtained by incrementing the value of the solution in the top-left diagonal cell by 1.
c. If the characters being compared in each string are unequal, the value of the cell is obtained by finding the maximum value between the value in the top cell and the left cell.
Return the value of the last cell in the table.
Subsequence Checking Algorithm
Store the length of the smaller string in a variable.
Call the helper function and check if the resulting value is equal to the length of the smaller string.
a. If the values are equal, return true.
b. If the values are unequal, return false.
Code
Solution 1
Solution 2
Last updated
Was this helpful?