Is Subsequence
LeetCode 392 · सोपा · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू
Greedy two-pointer match.
मी 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
i over s, j over t.
- 2
Match → advance both; otherwise advance only j.
- 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.