From f886424496b704c40204a3b6307b964672677395 Mon Sep 17 00:00:00 2001 From: KyrieJ95 Date: Sat, 7 Sep 2024 11:14:38 +0900 Subject: [PATCH 1/2] Valid-palindrome solution --- valid-palindrome/joon.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 valid-palindrome/joon.py diff --git a/valid-palindrome/joon.py b/valid-palindrome/joon.py new file mode 100644 index 000000000..47d77d2bf --- /dev/null +++ b/valid-palindrome/joon.py @@ -0,0 +1,19 @@ +import re + + +class Solution: + # Time: O(n) + # Space: O(1) + def isPalindrome(self, s: str) -> bool: + # 1. Convert string + # Time: O(n) + # Space: O(n) since re.sub() will internally use a new string of length n. + s = re.sub('[^a-z0-9]', '', s.lower()) + length = len(s) + # 2. Check if the string reads the same forward and backward, one by one. + # Time: O(n) + # Space: O(1) + for i in range(length): + if (s[i] != s[length - 1 - i]): + return False + return True From 34fc9da9e3a5b2b3fcd9174b00d9082a970a2f49 Mon Sep 17 00:00:00 2001 From: KyrieJ95 Date: Sat, 7 Sep 2024 11:32:16 +0900 Subject: [PATCH 2/2] Add missing-number solution --- missing-number/joon.py | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 missing-number/joon.py diff --git a/missing-number/joon.py b/missing-number/joon.py new file mode 100644 index 000000000..11d053b5f --- /dev/null +++ b/missing-number/joon.py @@ -0,0 +1,11 @@ +from typing import List + + +class Solution: + # Time: O(n) + # Space: O(1) + def missingNumber(self, nums: List[int]) -> int: + n = len(nums) + # MissingNumber = (Sum of 1, 2, ..., n) - Sum of nums) + # Time: O(n) + return n * (n + 1) // 2 - sum(nums)