Solution) class Solution: def isIsomorphic(self, s: str, t: str) -> bool: dic = {} for i in range(len(s)): if s[i] not in dic: dic[s[i]] = t[i] else: if dic[s[i]] != t[i]: return False countDic = Counter(dic.values()) for count in countDic.values(): if count > 1: return False return True Problem: LeetCode Time Complexity: O(n) Space Complexity: O(n) Explanation) First, using a dictionary, set ch..
Solution)class Solution: def canConstruct(self, ransomNote: str, magazine: str) -> bool: magDic = {} for char in magazine: if char in magDic: magDic[char] += 1 else: magDic[char] = 1 for char in ransomNote: if char in magDic: magDic[char] -= 1 if magDic[char] == 0: del magDic[char] else: return False return True Problem: LeetCode Time Complexity: O(m+n) Space Complexity: O(m)Explanation)I think ..
Solution) class Solution: def isSubsequence(self, s: str, t: str) -> bool: original, sub = 0, 0 while sub < len(s) and original < len(t): if s[sub] == t[original]: sub += 1 original += 1 if sub == len(s): return True return False Problem: LeetCode Time Complexity: O(m+n) Space Complexity: O(1) Explanation) There are two pointers sub and original that will iterate s and t. We're using two pointer..
Solution) class Solution: def lengthOfLastWord(self, s: str) -> int: res = 0 for i in range(len(s)-1, -1, -1): if s[i].isalpha(): res += 1 else: if res: break return res Problem: LeetCode Time Complexity: O(n) Space Complexity: O(1) Explanation) We'll iterate through s backwards to find the last word. When we find the last word, we will count how many characters the last word has and return that..
Solution) class Solution: def removeElement(self, nums: List[int], val: int) -> int: res = 0 left, right = 0, len(nums)-1 while left int: res = 0 left, right = 0, len(nums)-1 while left
Solution) class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: fast, slow = head, head while fast and fast.next: fast = fast.next.next slow = slow.next prev = None while slow: temp = slow.next slow.next = prev prev = slow slow = temp left, right = head, prev while right: if left.val != right.val: return False left = left.next right = right.next return True Time Complexity: O..
정답) class Solution: def containsDuplicate(self, nums: List[int]) -> bool: dupSet = set() for n in nums: if n in dupSet: return True else: dupSet.add(n) return False 문제) 문제출처 정수 배열 nums가 주어졌을 때, 배열의 어떤 원소가 적어도 두 번 존재한다면 True를 리턴하시오. 만약 모든 원소가 한 번씩밖에 존재하지 않는다면 False를 리턴하시오. 예시 1) nums = [1, 2, 3, 1] output = True 예시 2) nums = [1, 2, 3, 4] output = False 예시 1) nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2] ..
정답) class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: newTail = None newHead = None while head: newHead = ListNode(head.val) newHead.next = newTail head = head.next newTail = newHead return newHead 문제) 문제출처 Singly linked list의 head가 주어졌을 때, 이 리스트가 거꾸로 변환된 리스트를 리턴하시오. 예시 1) 예시 2) 예시 3) head = [] output = [] 제한) 리스트에는 0~5000개의 노드가 존재합니다. -5000 Optional[ListNode..
정답) class Solution: def isHappy(self, n: int) -> bool: res = 0 seen = set() while res != 1: res = 0 while n: res += (n % 10)**2 n //= 10 if res in seen: return False seen.add(res) n = res return True 문제) 문제출처 숫자 n이 행복한지 결정하는 알고리즘을 작성하시오. 행복한 숫자는 다음과 같을 때 결정됩니다: - 아무 양수가 주어졌을 때 양수의 각 자릿수를 제곱한 값들을 더하시오 - 위 단계를 합이 1이 될 때까지 반복하시오. 1이 나오지 않는 경우에는 같은 숫자들이 반복해서 나옵니다. - 각 자릿수의 합이 1로 끝나는 숫자가 행복한 숫자입니다. 행..
정답) class Solution: def hammingWeight(self, n: int) -> int: res = 0 for _ in range(32): if n & 1 == 1: res += 1 n >>= 1 return res 문제) 문제출처 2진수로 표현된 정수를 받아 그 2진수에서 1이 몇 개 있는지 리턴하는 함수를 만드시오. 예시 1) n = 00000000000000000000000000001011 output = 3 예시 2) n = 00000000000000000000000010000000 output = 1 예시 3) n = 11111111111111111111111111111101 output = 31 제한) 인풋의 길이는 32입니다. 풀이) 저번 문제와 비슷하게 &연산자와 >>연산..