일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 알고리즘
- codeup
- GenAI
- 파이썬
- nlp
- 생성형AI
- dfs
- two-pointer
- tree
- Stack
- LeetCode
- sql코테
- 코드업
- heap
- SQL
- Python
- 릿코드
- medium
- GenerativeAI
- 자연어처리
- 파이썬기초100제
- 파이썬알고리즘
- stratascratch
- gcp
- 투포인터
- 리트코드
- slidingwindow
- 슬라이딩윈도우
- Python3
- 니트코드
Archives
- Today
- Total
Tech for good
[Leetcode/TwoPointer] 392. Is Subsequence 본문
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
i, j = 0, 0 # Pointers for s and t
while i < len(s) and j < len(t):
if s[i] == t[j]: # Match found, move s pointer
i += 1
j += 1 # Always move t pointer
return i == len(s) # If we have matched all chars in s, return True
- i += 1 -> s의 인덱스를 증가
- j += 1 -> s와 상관없이 (즉, 매치됨과 상관없이) t의 인덱스를 하나씩 증가
- Example 2의 경우, x가 t에 없으므로 i < len(s) -> False
시간 복잡도 및 공간 복잡도
- 시간 복잡도: O(n) (t의 길이만큼 한 번 순회)
- 공간 복잡도: O(1) (추가적인 공간 사용 없음)