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.