From 4f4795bac9f25ae71612ab70b0a3a80869a6ea01 Mon Sep 17 00:00:00 2001 From: Taehun Lim Date: Sun, 8 Dec 2024 16:10:07 +0900 Subject: [PATCH 1/2] [First week][contains-duplicate] adds answer --- contains-duplicate/routiful.py | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 contains-duplicate/routiful.py diff --git a/contains-duplicate/routiful.py b/contains-duplicate/routiful.py new file mode 100644 index 000000000..b708501c9 --- /dev/null +++ b/contains-duplicate/routiful.py @@ -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 From 5d3df3d006e0a30fddf2eb9b7c2de433cff37063 Mon Sep 17 00:00:00 2001 From: Taehun Lim Date: Sun, 15 Dec 2024 20:24:15 +0900 Subject: [PATCH 2/2] [First week][valid-palindrome] adds answer --- valid-palindrome/routiful.py | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 valid-palindrome/routiful.py diff --git a/valid-palindrome/routiful.py b/valid-palindrome/routiful.py new file mode 100644 index 000000000..bff3b1a86 --- /dev/null +++ b/valid-palindrome/routiful.py @@ -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