Tensorum
YouTubeसीखना शुरू करें
LeetCode 392 · Two Pointers

Is Subsequence

LeetCode 392 · आसान · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Greedy two-pointer match.

आसानTwo PointersStringDynamic Programming

मैंने Is Subsequence (LeetCode 392) ऐसे हल किया — Two Pointers पैटर्न का एक आसान प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

Scan t once; whenever its character matches the next needed character of s, advance in s. If you consume all of s, it's a subsequence.

Approach

  1. 1

    i over s, j over t.

  2. 2

    Match → advance both; otherwise advance only j.

  3. 3

    Return i == len(s).

मैंने कैसे हल किया

Two pointersPythonO(n) time · O(1) space
class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        i=0
        j=0
        while i<len(s) and j<len(t):
            if s[i]==t[j]:
                i+=1
                j+=1
            else:
                j+=1
        return i==len(s)
स्टेप-थ्रू लोड हो रहा है…

Is Subsequence — अक्सर पूछे जाने वाले प्रश्न

Is Subsequence के पीछे intuition क्या है?

Scan t once; whenever its character matches the next needed character of s, advance in s. If you consume all of s, it's a subsequence.

Is Subsequence (LeetCode 392) कैसे हल करें?

1. i over s, j over t. 2. Match → advance both; otherwise advance only j. 3. Return i == len(s).

Is Subsequence की time और space complexity क्या है?

Time: O(|t|)। Space: O(1)। (Accepted approach: Two pointers।)

Is Subsequence में आम गलतियाँ क्या हैं?

An empty s is always a subsequence.