diff --git a/contains-duplicate/EstherKim97.py b/contains-duplicate/EstherKim97.py new file mode 100644 index 000000000..e88ccb839 --- /dev/null +++ b/contains-duplicate/EstherKim97.py @@ -0,0 +1,10 @@ +# 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. + +def containsDuplicate(nums): + seen = set() + for num in nums: + if num in seen: + return True + seen.add(num) + return False + diff --git a/top-k-frequent-elements/EstherKim97.py b/top-k-frequent-elements/EstherKim97.py new file mode 100644 index 000000000..cef0b6cd7 --- /dev/null +++ b/top-k-frequent-elements/EstherKim97.py @@ -0,0 +1,16 @@ +class Solution(object): + def topKFrequent(self, nums, k): + unique_no = set(nums) + data = [] + + for i in unique_no: + count = 0 + for j in nums: + if i == j: + count += 1 + data.append((count, i)) + + data = sorted(data, reverse=True, key=lambda x:[1]) + + return [x[1] for x in data[:k]] + diff --git a/valid-palindrome/EstherKim97.py b/valid-palindrome/EstherKim97.py new file mode 100644 index 000000000..4630ab127 --- /dev/null +++ b/valid-palindrome/EstherKim97.py @@ -0,0 +1,12 @@ +class Solution(object): + def isPalindrome(self, s): + import re + # remove all non-alphanumeric characters + s = re.sub(r'\W+', '', s.lower()) + + # check if the string is equal forward and backward + for i in range(len(s)//2): + if s[i] != s[-(i+1)]: + return False + return True +