Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 00677e9

Browse files
updates
1 parent 971f133 commit 00677e9

File tree

3 files changed

+44
-19
lines changed

3 files changed

+44
-19
lines changed

‎DSA_RoadMap/04_Group_Anagrams.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,29 @@ def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
1111
ress = list(res.values())
1212
return ress
1313

14+
from typing import List
15+
16+
# ---------------------- Solution 2 -----------------------
17+
18+
19+
class Solution:
20+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
21+
mp = {}
22+
ans =[]
23+
for s in strs:
24+
sorted_str = ''.join(sorted(s))
25+
26+
if sorted_str in mp:
27+
ans[mp[sorted_str]].append(s)
28+
29+
else:
30+
mp[sorted_str] = len(ans)
31+
ans.append([s])
32+
33+
return ans
34+
1435

36+
# Testing the solution
1537

1638

1739

‎DSA_RoadMap/05_347. TopKFrequentElements.py‎

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections import Counter, defaultdict
2+
from typing import List
3+
4+
class Solution:
5+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
6+
counted_nums = Counter(nums)
7+
most_common = counted_nums.most_common(k)
8+
result = []
9+
for key, freqeuncy in most_common:
10+
result.append(key)
11+
return result
12+
13+
14+
15+
16+
17+
# nums = [1]
18+
# nums = [1,1,1,2,2,3,4,4]
19+
# nums = [5,3,1,1,1,3,73,1]
20+
nums = [7,5,9,9,4,69,4,4]
21+
k = 2
22+
print(Solution().topKFrequent(nums,k))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /