LeetCode/Top Interview 150 106

290. Word Pattern

290. Word Pattern class Solution: def wordPattern(self, pattern: str, s: str) -> bool: # 문자열 s를 공백(" ") 기준으로 나누어 단어 리스트로 변환 s = s.split(" ") # pattern과 s의 길이가 다르거나 둘 중 하나가 비어있으면 False 반환 if len(pattern) != len(s) or len(pattern) == 0 or len(s) == 0: return False # pattern -> 단어 매핑과 단어 -> pattern 매핑을 추적하기 위한 딕셔너리 mapPatternS, mapSpattern = {..

205. Isomorphic Strings

205. Isomorphic Strings https://youtu.be/7yF-U1hLEqQ?si=6-1KxuCQXX5lnviA class Solution: def isIsomorphic(self, s: str, t: str) -> bool: # 두 문자열의 길이가 다르거나, 둘 중 하나라도 비어있으면 False 반환 if len(s) != len(t) or len(s) == 0 or len(t) == 0: return False # s -> t 매핑과 t -> s 매핑을 추적하기 위한 딕셔너리 생성 mapST, mapTS = {}, {} # 문자열 s와 t를 동시에 순회 for i in range(le..

383. Ransom Note

383. Ransom Note class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: # magazine에 있는 문자들의 빈도를 저장할 딕셔너리 countM = {} # magazine 문자열에서 각 문자의 빈도를 계산 for c in magazine: countM[c] = 1 + countM.get(c, 0) # 딕셔너리에 c가 없으면 기본값은 0 # ransomNote 문자열의 각 문자를 확인 for w in ransomNote: # 현재 문자가 magazine에서 충분히 남아있는지 확인 ..

48. Rotate Image

48. Rotate Image https://youtu.be/fMSJSS7eO1w?si=XXhhhqR7m4_ogJ2l https://youtu.be/I5WtA1Pax7Q?si=B9ySN5uPTH9Huk8R from typing import Listclass Solution: def rotate(self, matrix: List[List[int]]) -> None: """ 주어진 n x n 2D 행렬을 90도 시계 방향으로 회전합니다. 이 작업은 제자리에서(in-place) 수행되어야 하며, 추가적인 2D 배열을 사용할 수 없습니다. """ l, r = 0, len(matrix) - 1 # 왼쪽과 오른쪽 경계를 설정 whil..

36. Valid Sudoku

36. Valid Sudoku https://youtu.be/TjFXEUCMqI8?si=mfzpl_jlRUHMsnOR from typing import Listimport collectionsclass Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: # 각 열에 있는 숫자를 추적하기 위한 딕셔너리 (기본값은 set) cols = collections.defaultdict(set) # 각 행에 있는 숫자를 추적하기 위한 딕셔너리 (기본값은 set) rows = collections.defaultdict(set) # 각 3x3 서브그리드에 있는 숫자를 추적하기 위한 딕셔..

3. Longest Substring Without Repeating Characters

3. Longest Substring Without Repeating Characters https://youtu.be/wiGpQwVHdE0?si=b-6NK2xTHLhlVE5- class Solution: def lengthOfLongestSubstring(self, s: str) -> int: # 중복되지 않은 문자를 저장할 집합 (현재 윈도우의 문자들) charSet = set() # 윈도우의 왼쪽 포인터 l = 0 # 결과값(최대 길이)을 저장할 변수 res = 0 # 윈도우의 오른쪽 포인터를 문자열의 각 문자에 대해 순회 for r in range(len(s)): # s[r..