Skip to content

Week4 - 2 / 5 problems #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions missing-number/joon.py
Original file line number Diff line number Diff line change
@@ -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)
19 changes: 19 additions & 0 deletions valid-palindrome/joon.py
Original file line number Diff line number Diff line change
@@ -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