LeetCode 392: Determine if a String is a Subsequence Using the Two‑Pointer Technique
This article humorously references recent layoff memes before presenting a detailed tutorial on solving LeetCode problem 392—checking whether string s is a subsequence of t—using a two‑pointer algorithm, complete with step‑by‑step explanation, complexity analysis, visual illustrations, and reference implementations in C++, Java, and Python.
Recently a popular meme asks "What does it mean to be laid off until the major artery?" and the post shares several funny layoff anecdotes before shifting to a serious technical tutorial.
The author emphasizes the importance of continuous learning and skill upgrades in a rapidly changing job market.
Determine Subsequence
Problem (LeetCode 392) : Given strings s and t, determine whether s is a subsequence of t. A subsequence is formed by deleting zero or more characters without changing the order of the remaining characters.
Approach
Initialize two pointers i and j at the start of s and t respectively.
Traverse t with j. If s[i] equals t[j], increment i. In any case, increment j.
After the loop, if i has reached the end of s, s is a subsequence of t; otherwise it is not.
The time complexity of this method is O(n+m), where n and m are the lengths of s and t respectively.
Demonstration Process
The following images illustrate each step of the two‑pointer traversal for the first example (s = "abc", t = "ahbgdc").
Initial state: i points to the first character of s, j points to the first character of t.
Both characters match, move i and j.
Characters do not match, move only j.
Match again, move both pointers.
Continue until i reaches the end of s, confirming it is a subsequence.
Reference Code
C++
#include <iostream>
using namespace std;
class Solution {
public:
bool isSubsequence(string s, string t) {
int i = 0; // s index
int j = 0; // t index
while (i < s.size() && j < t.size()) {
if (s[i] == t[j]) {
i++; // move s pointer on match
}
j++; // always move t pointer
}
return i == s.size(); // true if s fully matched
}
};
int main() {
Solution solution;
string s = "abc";
string t = "ahbgdc";
cout << "Input: s = \"" << s << "\", t = \"" << t << "\"" << endl;
cout << "Output: " << (solution.isSubsequence(s, t) ? "true" : "false") << endl;
return 0;
}Java
public class Solution {
public boolean isSubsequence(String s, String t) {
int i = 0; // s index
int j = 0; // t index
while (i < s.length() && j < t.length()) {
if (s.charAt(i) == t.charAt(j)) {
i++; // move s pointer on match
}
j++; // always move t pointer
}
return i == s.length(); // true if s fully matched
}
public static void main(String[] args) {
Solution solution = new Solution();
String s = "abc";
String t = "ahbgdc";
System.out.println("Input: s = \"" + s + "\", t = \"" + t + "\"");
System.out.println("Output: " + (solution.isSubsequence(s, t) ? "true" : "false"));
}
}Python
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
i = 0 # s index
j = 0 # t index
# traverse t with two pointers
while i < len(s) and j < len(t):
if s[i] == t[j]:
i += 1 # move s pointer on match
j += 1 # always move t pointer
return i == len(s) # true if s fully matched
if __name__ == "__main__":
solution = Solution()
s = "abc"
t = "ahbgdc"
print(f"Input: s = \"{s}\", t = \"{t}\"")
print("Output:", solution.isSubsequence(s, t))Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
