Skip to content
Merged
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
37 changes: 37 additions & 0 deletions contains-duplicate/routiful.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# FIRST WEEK

# Question : 217. Contains Duplicate
# Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

# Example 1:
# Input: nums = [1,2,3,1]
# Output: true
# Explanation:
# The element 1 occurs at the indices 0 and 3.

# Example 2:
# Input: nums = [1,2,3,4]
# Output: false
# Explanation:
# All elements are distinct.

# Example 3:
# Input: nums = [1,1,1,3,3,4,3,2,4,2]
# Output: true

# Constraints:
# 1 <= nums.length <= 105
# -109 <= nums[i] <= 109

# Notes:
# Counts every elements(in the list) using 'count()' API. Luckily it is less than O(n) but maximum O(n)
# Makes Dict; the key is elements of the list. If the length of them are different, the list has duplicate elements. It is O(n)

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
d = {}
for n in nums:
d[n] = 1
if len(d) != len(nums):
return True
return False
59 changes: 59 additions & 0 deletions valid-palindrome/routiful.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# FIRST WEEK

# Question : 125. Valid Palindrome

# A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and
# removing all non-alphanumeric characters, it reads the same forward and backward.
# Alphanumeric characters include letters and numbers.
# Given a string s, return true if it is a palindrome, or false otherwise.

# Example 1:
# Input: s = "A man, a plan, a canal: Panama"
# Output: true
# Explanation: "amanaplanacanalpanama" is a palindrome.

# Example 2:
# Input: s = "race a car"
# Output: false
# Explanation: "raceacar" is not a palindrome.

# Example 3:
# Input: s = " "
# Output: true
# Explanation: s is an empty string "" after removing non-alphanumeric characters.
# Since an empty string reads the same forward and backward, it is a palindrome.

# Constraints:
# 1 <= s.length <= 2 * 105
# s consists only of printable ASCII characters.

# Notes:
# Using `reverse`(O(N)) api and matching all(max O(N)) look straightforward.
# The two pointer method may useful to decrease time complexity.
# If length of input is 0 or 1, return true.

class Solution:
def isPalindrome(self, s: str) -> bool:
l = len(s)
if l < 2:
return True

f = 0
b = l - 1

while (f <= b):
if (s[f] == " " or (s[f].isalpha() == False and s[f].isnumeric() == False)):
f = f + 1
continue

if (s[b] == " " or (s[b].isalpha() == False and s[b].isnumeric() == False)):
b = b - 1
continue

if s[f].lower() != s[b].lower():
return False

f = f + 1
b = b - 1

return True
Loading