Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 392 · Two Pointers

Is Subsequence

LeetCode 392 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

Greedy two-pointer match.

எளிதுTwo PointersStringDynamic Programming
LeetCode இல் திற ↗

நான் 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.