Skip to content

Commit 61ce45c

Browse files
committed
Day_1 continuation
1 parent d379217 commit 61ce45c

File tree

864 files changed

+138311
-42
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

864 files changed

+138311
-42
lines changed

leetcode-master/arrays_and_hashing/contains_duplicate.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
-109 <= nums[i] <= 109
2626
"""
2727

28+
# Solution by rubix-coder
29+
class Solution4:
30+
def containsDuplicate(self, nums: List[int]) -> bool:
31+
return len(set(nums))!= len(nums)
32+
33+
"""
34+
419 ms Beats 51.40%
35+
31.95 MB Beats 44.45%
36+
"""
37+
2838
from typing import List
2939

3040

@@ -78,12 +88,4 @@ def containsDuplicate(self, nums: List[int]) -> bool:
7888
28.32 MB Beats 93.65 of users with Python3
7989
"""
8090

81-
# Solution 4 (JP)
82-
class Solution4:
83-
def containsDuplicate(self, nums: List[int]) -> bool:
84-
return len(set(nums))!= len(nums)
8591

86-
"""
87-
419 ms Beats 51.40%
88-
31.95 MB Beats 44.45%
89-
"""

leetcode-master/arrays_and_hashing/group_anagrams.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
11
"""
22
URL: https://leetcode.com/problems/group-anagrams/
3+
Given an array of strings strs, group the
4+
anagrams together. You can return the answer in any order.
35
6+
Example 1:
7+
8+
Input: strs = ["eat","tea","tan","ate","nat","bat"]
9+
10+
Output: [["bat"],["nat","tan"],["ate","eat","tea"]]
11+
12+
Explanation:
13+
14+
There is no string in strs that can be rearranged to form "bat".
15+
The strings "nat" and "tan" are anagrams as they can be rearranged to form each other.
16+
The strings "ate", "eat", and "tea" are anagrams as they can be rearranged to form each other.
17+
Example 2:
18+
19+
Input: strs = [""]
20+
21+
Output: [[""]]
22+
23+
Example 3:
24+
25+
Input: strs = ["a"]
26+
27+
Output: [["a"]]
28+
29+
30+
31+
Constraints:
32+
33+
1 <= strs.length <= 104
34+
0 <= strs[i].length <= 100
35+
strs[i] consists of lowercase English letters.
436
"""
37+
538
from collections import defaultdict
639
from typing import List
740

41+
#solution by rubix-coder
42+
from collections import defaultdict
43+
class Solution5:
44+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
45+
anagram_dict = defaultdict(list)
46+
result = []
47+
48+
for s in strs:
49+
sorted_s = tuple(sorted(s))
50+
anagram_dict[sorted_s].append(s)
51+
52+
return list(anagram_dict.values())
53+
54+
# https://leetcode.com/problems/group-anagrams/submissions/1455490795
55+
56+
857

958
# Solution 1:
1059
class Solution:

leetcode-master/arrays_and_hashing/two_sum.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@
3636
nums = [2, 7, 11, 15]
3737
target = 9
3838

39+
# Solution by rubix-coder:
40+
class Solution_3:
41+
def twoSum(nus, target):
42+
cleaned = []
43+
for i in nus:
44+
if i <= target:
45+
cleaned.append(i)
46+
for i in cleaned:
47+
if target-cleaned[i] in cleaned:
48+
return [nus.index(cleaned[i]), nus.index(target-cleaned[i])]
49+
50+
51+
sol_3 = Solution_3.twoSum(nus = [1, 2, 3, 6], target = 9)
52+
print(sol_3)
3953

4054
class Solution:
4155
def twoSum(self, nums, target):
@@ -87,20 +101,3 @@ def twoSum(self, nums, target):
87101
Beats 6.37% of users with Python3
88102
"""
89103

90-
91-
# Solution 3 :
92-
93-
94-
class Solution_3:
95-
def twoSum(nus, target):
96-
cleaned = []
97-
for i in nus:
98-
if i <= target:
99-
cleaned.append(i)
100-
for i in cleaned:
101-
if target-cleaned[i] in cleaned:
102-
return [nus.index(cleaned[i]), nus.index(target-cleaned[i])]
103-
104-
105-
sol_3 = Solution_3.twoSum(nus = [1, 2, 3, 6], target = 9)
106-
print(sol_3)

leetcode-master/arrays_and_hashing/valid_anagram.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@
2020
1 <= s.length, t.length <= 5 * 104
2121
s and t consist of lowercase English letter
2222
"""
23+
import random
24+
source = ["cat","bat","car","anagram","hit","ball","food"]
25+
target = ["tac","tab","hall","garaman","food","lullaby","doof"]
26+
27+
from collections import Counter
28+
#Solution by rubix-coder
29+
class SolutionIsAnagram:
30+
def isAnagram(s: str, t: str) -> bool:
31+
return Counter(s)==Counter(t)
32+
33+
for i in range(200):
34+
s = random.choice(source)
35+
t = random.choice(target)
36+
test = SolutionIsAnagram.isAnagram(s=s,t=t)
37+
print(f"Are the words {s} and {t} are anagrams ? -> {test}")
2338

2439

2540
class Solution1:
@@ -53,10 +68,6 @@ def isAnagram(self, s: str, t: str) -> bool:
5368
s = "aacc"
5469
t = "ccac"
5570

56-
sol = Solution()
57-
print(sol.isAnagram(s, t))
58-
59-
6071

6172
class Solution2:
6273
def isAnagram(self, s: str, t: str) -> bool:
@@ -129,16 +140,3 @@ def isAnagram(self, s: str, t: str) -> bool:
129140

130141

131142

132-
from collections import defaultdict
133-
class Solution5:
134-
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
135-
anagram_dict = defaultdict(list)
136-
result = []
137-
138-
for s in strs:
139-
sorted_s = tuple(sorted(s))
140-
anagram_dict[sorted_s].append(s)
141-
142-
return list(anagram_dict.values())
143-
144-
# https://leetcode.com/problems/group-anagrams/submissions/1455490795

0 commit comments

Comments
 (0)