26. Check if a String is Subsequence of Other
β GFG solution to check if one string is a subsequence of another using efficient two-pointer technique. Find if s1 is subsequence of s2 optimally. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given two strings s1
and s2
, you need to check whether s1
is a subsequence of s2
or not.
Note: A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
π Examples
Example 1
Input: s1 = "AXY", s2 = "YADXCP"
Output: false
Explanation: s1 is not a subsequence of s2 as 'Y' appears before 'A' in s2,
but in s1, 'A' comes before 'Y'.
Example 2
Input: s1 = "gksrek", s2 = "geeksforgeeks"
Output: true
Explanation: If we combine the bold characters of "g**e**e**k**sfor**g**ee**k**s",
it gives us "geksrek". We can derive "gksrek" by removing one 'e', making s1 a subsequence of s2.
Example 3
Input: s1 = "abc", s2 = "aec"
Output: false
Explanation: Character 'b' is missing in s2, so s1 cannot be a subsequence of s2.
π Constraints
$1 \le \text{s1.size()}, \text{s2.size()} \le 10^6$
β
My Approach
The optimal approach uses the Two Pointer Technique to efficiently check if one string is a subsequence of another:
Two Pointer Greedy Algorithm
Initialize Pointers:
Use pointer
i
for strings1
(subsequence to find).Traverse string
s2
using a range-based loop or second pointer.
Character Matching:
For each character in
s2
, check if it matches the current character ins1
(at positioni
).If characters match, advance pointer
i
to look for the next character ins1
.
Greedy Strategy:
Always take the first available match in
s2
for each character ins1
.This greedy approach is optimal because taking later matches cannot give better results.
Final Check:
If we've successfully matched all characters in
s1
(i.e.,i == s1.length()
), thens1
is a subsequence ofs2
.
Early Termination:
We can stop as soon as we've found all characters of
s1
, even ifs2
has more characters.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(m), where m is the length of string s2. We traverse s2 exactly once, and for each character, we perform constant time operations.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for the pointer variable and loop counters.
π§βπ» Code (C++)
class Solution {
public:
bool isSubSeq(string& s1, string& s2) {
int i = 0;
for (char c : s2) {
if (i < s1.size() && s1[i] == c) {
i++;
}
}
return i == s1.size();
}
};
β Code (Java)
class Solution {
public boolean isSubSeq(String s1, String s2) {
int i = 0, n = s1.length(), m = s2.length();
for (int j = 0; j < m && i < n; j++) {
if (s1.charAt(i) == s2.charAt(j)) {
i++;
}
}
return i == n;
}
}
π Code (Python)
class Solution:
def isSubSeq(self, s1, s2):
i = 0
for c in s2:
if i < len(s1) and s1[i] == c:
i += 1
return i == len(s1)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated