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 971f133

Browse files
Update
1 parent 8e276d3 commit 971f133

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

‎DSA_RoadMap/04_Group_Anagrams.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
from typing import List
2-
2+
fromcollectionsimportdefaultdict
33
# ---------------------- Solution 1 -----------------------
44

55
class Solution:
66
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
7-
ls = []*len(strs)
8-
for i in range(len(strs)):
9-
sls = sorted(strs[i])
10-
for j in range(len(strs)):
11-
if sls == sorted(strs[j]):
12-
ls.append(ls[j]+ [strs[i]])
13-
return [x for x in ls if len(x)>0 ]
14-
15-
# Testing the solution
7+
res = defaultdict(list)
8+
n = len(strs)
9+
for word in strs:
10+
res[str(sorted(word))].append(word)
11+
ress = list(res.values())
12+
return ress
13+
14+
1615

1716

1817

1918

19+
strs = ["eat","tea","tan","ate","nat","bat"]
2020

21-
strs= ["eat","tea","tan","ate","nat","bat"]
21+
print(Solution().groupAnagrams(strs))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
class Solution:
4+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
5+
6+
res = set()
7+
for i in nums:
8+
if i <= k:
9+
if nums.count(i) > 1:
10+
res.add(i)
11+
if len(nums) == k:
12+
res.add(nums[i])
13+
return list(res)
14+
15+
16+
17+
nums = [1,1,1,2,2,3,4,4]
18+
k = 4
19+
print(Solution().topKFrequent(nums,k))

‎leecode/03_longest-substring-without-repeating-characters.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,4 @@ def lengthOfLongestSubstring(self, s: str) -> int:
77
start = seen[letter] + 1
88
result = max(result, i - start + 1)
99
seen[letter] = i
10-
return result
11-
12-
k = Solution()
13-
k.lengthOfLongestSubstring("abcabcbb")
10+
return result

0 commit comments

Comments
(0)

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