Skip to content

Commit ff9ea06

Browse files
committed
Update
1 parent 6b90562 commit ff9ea06

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

DSA_RoadMap/01_ContainsDupicate.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from typing import List
2+
3+
# ---------------------- Solution 1 -----------------------
4+
5+
# class Solution:
6+
# def containsDuplicate(self, nums: List[int]) -> bool:
7+
# n = len(nums)
8+
# for i in range(n):
9+
# for j in range(i+1,n):
10+
# # print(nums[i], nums[j])
11+
# if nums[i] == nums[j]:
12+
# return True
13+
# return False
14+
15+
# ---------------------- Solution 2 -----------------------
16+
17+
# class Solution:
18+
# def containsDuplicate(self, nums: List[int]) -> bool:
19+
# seen = set()
20+
# print(seen)
21+
# for num in nums:
22+
# if num in seen:
23+
# return True
24+
# seen.add(num)
25+
# return False
26+
27+
# ---------------------- Solution 3 -----------------------
28+
29+
30+
# class Solution:
31+
# def containsDuplicate(self, nums: List[int]) -> bool:
32+
# hs = set(nums)
33+
# return len(hs) != len(nums)
34+
35+
36+
# ---------------------- Solution 4 -----------------------
37+
38+
# class Solution:
39+
# def containsDuplicate(self, nums: List[int]) -> bool:
40+
# nset = set()
41+
# for num in nums:
42+
# if num in nset:
43+
# return True
44+
# nset.add(num)
45+
# return False
46+
47+
# ---------------------- Solution 5 -----------------------
48+
class Solution:
49+
def containsDuplicate(self, nums: List[int]) -> bool:
50+
nd =len(set(nums))
51+
if len(nums) == nd:
52+
return False
53+
else:
54+
return True
55+
56+
nums = [1,2,3,1]
57+
print(Solution().containsDuplicate(nums))

DSA_RoadMap/02_ValidAnagram.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
# ---------------------- Solution 1 -----------------------
3+
4+
5+
# class Solution:
6+
# def isAnagram(self, s: str, t: str) -> bool:
7+
# if len(s) == len(t):
8+
# if sorted(s) == sorted(t):
9+
# return True
10+
# return False
11+
12+
# ---------------------- Solution 2 -----------------------
13+
14+
15+
# class Solution:
16+
# def isAnagram(self, s: str, t: str) -> bool:
17+
# if len(s) != len(t):
18+
# return False
19+
20+
# freq1 = Counter(s)
21+
# freq2 = Counter(t)
22+
23+
# for i in freq1:
24+
# if freq1[i] != freq2[i]:
25+
# return False
26+
27+
# return True
28+
29+
# ---------------------- Solution 3 -----------------------
30+
31+
32+
class Solution:
33+
def isAnagram(self, s: str, t: str) -> bool:
34+
return sorted(s) == sorted(t)
35+
36+
s1 = "anagram"
37+
t1 = "nagaran"
38+
print(Solution().isAnagram(s1, t1))

DSA_RoadMap/03_TwoSum.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import List
2+
3+
# class Solution:
4+
# def twoSum(self, nums: List[int], target: int) -> List[int]:
5+
# n = len(nums)
6+
# for i in range(n):
7+
# for num2 in range(i+1, n):
8+
# print(nums[i] , nums[num2])
9+
# if nums[i] + nums[num2] == target:
10+
# return [i, num2]
11+
# # raise ValueError("No solution found")
12+
13+
14+
class Solution:
15+
def twoSum(self, nums: List[int], target: int) -> List[int]:
16+
hmap = {}
17+
for i in range(len(nums)):
18+
hmap[nums[i]] = i
19+
for i in range(len(nums)):
20+
complement = target - nums[i]
21+
if complement in hmap and hmap[complement] != i:
22+
return [i, hmap[complement]]
23+
24+
25+
26+
27+
nums = [3,2,4]
28+
target = 6
29+
30+
print(Solution().twoSum(nums, target))

0 commit comments

Comments
 (0)