LeetCode 392: Is Subsequence Solution
Master LeetCode problem 392 (Is Subsequence), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
392. Is Subsequence
Problem Explanation
Explanation
To solve this problem, we can use a two-pointer approach. We iterate through both strings s and t using two pointers i and j. If the characters at the current positions match, we move both pointers forward. If not, we only move the pointer for t. If we can reach the end of s, it means s is a subsequence of t.
- Time complexity: O(n), where n is the length of string
t. - Space complexity: O(1).
Solution Code
class Solution {
public boolean isSubsequence(String s, String t) {
int i = 0, j = 0;
while (i < s.length() && j < t.length()) {
if (s.charAt(i) == t.charAt(j)) {
i++;
}
j++;
}
return i == s.length();
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 392 (Is Subsequence)?
This page provides optimized solutions for LeetCode problem 392 (Is Subsequence) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 392 (Is Subsequence)?
The time complexity for LeetCode 392 (Is Subsequence) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 392 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 392 in Java, C++, or Python.