diff --git a/climbing-stairs/jylee2033.py b/climbing-stairs/jylee2033.py new file mode 100644 index 000000000..a60bdb239 --- /dev/null +++ b/climbing-stairs/jylee2033.py @@ -0,0 +1,20 @@ +class Solution: + def climbStairs(self, n: int) -> int: + # ways(n) = ways(n-1) + ways(n-2) + + if n == 1: + return 1 + elif n == 2: + return 2 + + ways = [0] * (n + 1) + ways[1] = 1 + ways[2] = 2 + + for i in range(3, n + 1): + ways[i] = ways[i - 1] + ways[i - 2] + + return ways[n] + +# Time Complexity: O(n) +# Space Complexity: O(n) diff --git a/product-of-array-except-self/jylee2033.py b/product-of-array-except-self/jylee2033.py new file mode 100644 index 000000000..6f98479a4 --- /dev/null +++ b/product-of-array-except-self/jylee2033.py @@ -0,0 +1,23 @@ +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + # answer[i] = product up to i * product from i + + n = len(nums) + + prefix_product = [1] * n + suffix_product = [1] * n + + for i in range(1, n): + prefix_product[i] = prefix_product[i-1] * nums[i-1] + + for i in range(n - 2, -1, -1): + suffix_product[i] = suffix_product[i + 1] * nums[i + 1] + + answer = [1] * n + for i in range(n): + answer[i] = prefix_product[i] * suffix_product[i] + + return answer + +# Time complexity: O(n) +# Space complexity: O(n) diff --git a/valid-anagram/jylee2033.py b/valid-anagram/jylee2033.py new file mode 100644 index 000000000..1bdd25451 --- /dev/null +++ b/valid-anagram/jylee2033.py @@ -0,0 +1,33 @@ +class Solution: + def isAnagram(self, s: str, t: str) -> bool: + # If lengths differ, they cannot be anagrams + if len(s) != len(t): + return False + + # Build a hashmap counting characters in t + char_count = {} + + # for letter in t: + # if letter not in char_count: + # char_count[letter] = 1 + # else: + # char_count[letter] += 1 + + for letter in t: + char_count[letter] = char_count.get(letter, 0) + 1 + + # Decrease counts using characters from s + for letter in s: + if letter not in char_count: + return False + + char_count[letter] -= 1 + + # If count becomes negative, s has extra characters + if char_count[letter] < 0: + return False + + return True + +# Time Complexity: O(n) +# Space Complexity: O(1) since the hashmap stores at most 26 letters