0% completed
Problem Statement
Given two strings s
and t
, return true
if the string s
is a subsequence
of the string t
. Otherwise, return false
.
A subsequence
of a string is a new string that can be formed
from the original string
by eliminating some
(can be zero) of the characters without changing
the order
of the remaining characters.
Examples
-
Example 1:
- Input:
s = "hello"
,t = "xyhealoloo"
- Expected Output: true
- Justification: By removing "x", "y", "a", "o", and the second "o" from
t
,s
can be obtained while maintaining the original order.
- Input:
-
Example 2:
- Input:
s = "axc"
,t = "ahbgdc"
- Expected Output: false
- Justification: There is no way to remove characters from
t
to achieves
without altering the sequence of "axc".
- Input:
-
Example 3:
- Input:
s = "abc"
,t = "aabc"
- Expected Output: true
- Justification: By removing the first "a" in
t
,s
is formed while preserving the character sequence.
- Input:
Solution
To solve this problem, we adopt a two-pointer technique that iteratively compares characters from s
and t
. One pointer is used to traverse s
, and another pointer for t
. The idea is to advance both pointers when characters match, indicating that part of the subsequence has been found. When characters do not match, only the pointer for t
advances, searching for the next potential matching character.
This method is effective because it linearly scans through both strings simultaneously, ensuring that the relative order of characters in s
is maintained in t
. By the end of the traversal, if we have successfully navigated through all characters in s
, it confirms that s
is indeed a subsequence of t
.
Step-by-Step Algorithm
-
Initialize Pointers: Start by initializing two pointers,
indexS
andindexT
, to 0. These pointers will be used to traverse through the stringss
andt
, respectively. -
Traverse Through
t
: Begin a loop that continues as long asindexT
is less than the length oft
andindexS
is less than the length ofs
. This loop will go through each character oft
to check againsts
. -
Check Character Match: Inside the loop, compare the current character of
t
(pointed to byindexT
) with the current character ofs
(pointed to byindexS
).- If the characters match, increment
indexS
by 1 to move to the next character ins
. This step is crucial as it indicates that part of the subsequence has been found.
- If the characters match, increment
-
Move to Next Character in
t
: After the check or in case of a mismatch, incrementindexT
by 1 to continue scanning throught
. -
Check Subsequence Found: After exiting the loop, check if
indexS
equals the length ofs
. This condition signifies that all characters ins
have been found int
in the correct order.- If
indexS
equals the length ofs
, returntrue
, indicatings
is a subsequence oft
. - If the loop ends and
indexS
is not equal to the length ofs
, returnfalse
, indicatings
is not a subsequence oft
.
- If
Algorithm Walkthrough
Let's consider the Input: s = "hello"
, t = "xyhealoloo"
- Initial Setup:
indexS = 0
,indexT = 0
.
- Step 1: Begin Loop
indexT = 0
,t[0] = 'x'
,indexS = 0
,s[0] = 'h'
. They do not match, so onlyindexT
is incremented.
- Step 2: Next Iteration
indexT = 1
,t[1] = 'y'
, still no match. IncrementindexT
.
- Step 3: Finding 'h'
indexT = 2
,t[2] = 'h'
, matchess[0] = 'h'
. Increment bothindexS
andindexT
.
- Step 4: Finding 'e'
- Continue to
indexT = 3
,t[3] = 'e'
, matchess[1] = 'e'
. Increment bothindexS
andindexT
.
- Continue to
- Step 5: Skipping to 'l'
indexT
increments through 'a', until it finds the first 'l' atindexT = 5
.s[2] = 'l'
matches, increment both.
- Step 6: Finding Second 'l'
indexT = 6
,t[6] = 'o'
, no match, incrementindexT
.indexT = 7
,t[7] = 'l'
, matchess[3] = 'l'
. Increment both.
- Step 7: Finding 'o'
indexT = 8
,t[8] = 'o'
, matchess[4] = 'o'
. Increment both.
- Step 8: Conclusion
- Now,
indexS = 5
, which equals the length ofs
. This means every character ins
has been found int
in order, confirmings
is a subsequence oft
.
- Now,
Code
Complexity Analysis
Time Complexity
The time complexity of the algorithm is O(n), where (n) is the length of string t
. This is because, in the worst case, we traverse the entire string t
once, comparing each character with characters from s
. The traversal and comparisons for each character occur in constant time.
Space Complexity
The space complexity is O(1). Only a fixed number of variables (indexS
and indexT
) are used, regardless of the input size. This makes the space complexity constant, as it does not scale with the size of the input strings.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible