Skip to content

Commit 6f9f85b

Browse files
committed
Add solutions for "Valid Palindrome" and "Top K Frequent Elements", update README
1 parent ff1a25a commit 6f9f85b

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
|0217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Python](./array_hashing/0217-contains-duplicate.py)|Array & Hashing| Easy |
88
|0242|[Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [Python](./array_hashing/0242-valid-anagram.py)|Array & Hashing| Easy |
99
|0001|[Two Sum](https://leetcode.com/problems/two-sum/description/) | [Python](./array_hashing/0001-two-sum.py)|Array & Hashing| Easy |
10-
|0049|[Group Anagrams](https://leetcode.com/problems/group-anagrams/description/) | [Python](./array_hashing/0049-group-anagrams.py)|Array & Hashing| Medium |
10+
|0049|[Group Anagrams](https://leetcode.com/problems/group-anagrams/description/) | [Python](./array_hashing/0049-group-anagrams.py)|Array & Hashing| Medium |
11+
|0125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) | [Python](./two_pointers/0125-valid-palindrome.py)|Two Pointers| Easy |
12+
|0347|[Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/description/) | [Python](./array_hashing/0347-top-k-frequent-elements.py)|Array & Hashing| Medium |

array_hashing/0125-valid-palindrome.py

Whitespace-only changes.

array_hashing/0347-top-k-frequent-elements.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,18 @@ def topKFrequent(self, nums, k):
55
:type k: int
66
:rtype: List[int]
77
"""
8+
freq_map = {}
9+
10+
for num in nums:
11+
if num not in freq_map:
12+
freq_map[num] = 0
13+
freq_map[num] += 1
14+
15+
sorted_items = sorted(freq_map.items(), key=lambda x: x[1], reverse=True)
16+
17+
res = [item[0] for item in sorted_items[:k]]
18+
19+
return res
20+
21+
822

stack/0020-valid-parentheses.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def isValid(self, s):
3+
"""
4+
:type s: str
5+
:rtype: bool
6+
"""
7+
if(len(s) %2 != 0): return False
8+
9+
stack = []
10+
11+
for c in s:
12+
if c in "{[(":
13+
stack.append(c)
14+
elif c in "}])":
15+
if not stack:
16+
return False
17+
if (c == "}" and stack[-1] == "{") or \
18+
(c == "]" and stack[-1] == "[") or \
19+
(c == ")" and stack[-1] == "("):
20+
stack.pop()
21+
else:
22+
return False
23+
24+
return len(stack) == 0
25+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def isPalindrome(self, s):
3+
"""
4+
:type s: str
5+
:rtype: bool
6+
"""
7+
8+
# new_s = ""
9+
10+
# for c in s:
11+
# if (c.isalnum() ): new_s += c
12+
13+
# new_s = new_s.lower()
14+
15+
# return new_s == new_s[::-1]
16+
17+
filtered_s = [c.lower() for c in s if c.isalnum()]
18+
return filtered_s == filtered_s[::-1]
19+

0 commit comments

Comments
 (0)