Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Is Subsequence

C++, Python and TypeScript solutions for Is Subsequence.

The Is Subsequence solutions are below.

Solution files

Python•is-subsequence/synced-solution.py
def isSubsequence(s, t):
    s_idx = 0

    for char in t:
        if s_idx < len(s) and char == s[s_idx]:
            s_idx += 1

    return s_idx == len(s)
C++•is-subsequence/synced-solution.cpp
class Solution {
public:
    bool isSubsequence(string s, string t) {
        int s_idx = 0;

        for (char c : t) {
            if (s_idx < s.length() && c == s[s_idx]) {
                s_idx++;
            }
        }

        return s_idx == s.length();
    }
};
TypeScript•is-subsequence/synced-solution.ts
function isSubsequence(s: string, t: string): boolean {
    let sIdx = 0;

    for (const char of t) {
        if (sIdx < s.length && char === s[sIdx]) {
            sIdx++;
        }
    }

    return sIdx === s.length;
}