DEV Community

Debesh P.
Debesh P.

Posted on

392. Is Subsequence | LeetCode | Top Interview 150 | Coding Questions

Problem Link

https://leetcode.com/problems/is-subsequence/


Detailed Step-by-Step Explanation

https://leetcode.com/problems/is-subsequence/solutions/7447048/most-optimal-solution-beats-200-on-o1-tw-6fm1


leetcode 392


Solution

class Solution {
    public boolean isSubsequence(String s, String t) {

        int i = 0;
        int j = 0;

        while (i < s.length() && j < t.length()) {
            if (s.charAt(i) == t.charAt(j)) {
                i++;
            }
            j++;
        }

        return i == s.length();
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)