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.