Skip to content

Commit 8092108

Browse files
authored
Add files via upload
1 parent 8e4fe8f commit 8092108

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
2+
3+
# Example 1:
4+
# Input: nums = [1,2,3,1]
5+
# Output: true
6+
# Explanation:
7+
# The element 1 occurs at the indices 0 and 3.
8+
9+
# Example 2:
10+
# Input: nums = [1,2,3,4]
11+
# Output: false
12+
# Explanation:
13+
# All elements are distinct.
14+
15+
# Example 3:
16+
# Input: nums = [1,1,1,3,3,4,3,2,4,2]
17+
# Output: true
18+
19+
# Constraints:
20+
# 1 <= nums.length <= 105
21+
# -109 <= nums[i] <= 109
22+
23+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------
24+
25+
# checking length of the set of the list and length of the list itself.
26+
# Set will remove duplicates, so if duplicates are present, lengths will not be equal so return True.
27+
# If lengths are equal, no duplicates = False
28+
29+
class Solution:
30+
def containsDuplicate(self, nums: List[int]) -> bool:
31+
if len(set(nums)) != len(nums):
32+
return True
33+
else:
34+
return False
35+
36+
# length of set is less than(or not equal to) length of list only when there are duplicates
37+
# so if this is the case, return True else return False
38+
39+
class Solution:
40+
def containsDuplicate(self, nums: List[int]) -> bool:
41+
return len(set(nums)) < len(nums):
42+
43+
class Solution:
44+
def containsDuplicate(self, nums: List[int]) -> bool:
45+
return len(set(nums)) != len(nums):
46+
47+
48+
Time Complexity = O(n)
49+
Space Complexity = O(n)
50+
51+
52+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------
53+
54+
# create an empty set- seen
55+
# iterate the list- if a number is already present in set, it means it's a duplicate, so return True
56+
# if a number is not present in the set, add it to the set
57+
# if ultimately True is not returned, return False
58+
59+
class Solution:
60+
def containsDuplicate(self, nums: List[int]) -> bool:
61+
seen = set()
62+
63+
for n in nums:
64+
if n in seen:
65+
return True
66+
seen.add(n)
67+
return False
68+
69+
Time Complexity = O(n)
70+
Space Complexity = O(n)
71+
72+
# Companies:
73+
# Google- 6
74+
# Amazon- 5
75+
# Bloomberg- 4
76+
# Microsoft- 3
77+
# Meta- 2
78+
# Yahoo- 3
79+
# Oracle- 2
80+
# Airbnb- 2
81+
# Palantir Technologies- 2
82+
# Paycom- 2
83+
# Apple- 31
84+
# Adobe- 21
85+
# Uber- 18
86+
# TCS- 7
87+
# Yandex- 3
88+
# JP Morgan- 2
89+
# EPAM Systems- 2
90+
# DE Shaw- 2
91+
# Siemens- 2
92+
# Accenture- 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Given two strings s and t, return true if t is an anagram of s, and false otherwise.
2+
3+
# Example 1:
4+
# Input: s = "anagram", t = "nagaram"
5+
# Output: true
6+
7+
# Example 2:
8+
# Input: s = "rat", t = "car"
9+
# Output: false
10+
11+
# Constraints:
12+
# 1 <= s.length, t.length <= 5 * 104
13+
# s and t consist of lowercase English letters.
14+
15+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------
16+
17+
# SORTING
18+
# compare the sorted strings
19+
# if the sorted strings are equal, they are anagrams, so return True, else return False
20+
# it is optional to check for lengths
21+
22+
class Solution:
23+
def isAnagram(self, s: str, t: str) -> bool:
24+
# if len(s) != len(t):
25+
# return False
26+
27+
return sorted(s) == sorted(t)
28+
29+
Time Complexity = O(nlogn + mlogm) where n is length of s and m is length of t
30+
Space Complexity = O(n + m) or O(1) depending on the sorting algorithm- (ChatGPT says O(n + m))
31+
32+
# -------------------------------------------------------------------------------------------------------------------------------------------------------------------
33+
34+
# HASH TABLES: Maintain a frequency table of each letter in the strings
35+
# first compare length of strings; if they are equal then go ahead
36+
# create 2 empty hash tables; 1 for each string
37+
# iterate through the length of the string(any string, because both lengths are same)
38+
# when the loop is on a character, add 1 to the count. But what if that character is met for the first time?
39+
# use hashtable.get() method, where 1st parameter is the character(s[i]), 2nd is the default value(0).
40+
# 0 because we are adding 1 to it when it's encountered.
41+
# compare both the hashtables. If they are not equal, they are not anagrams, so return False
42+
43+
class Solution:
44+
def isAnagram(self, s: str, t: str) -> bool:
45+
if len(s) != len(t):
46+
return False
47+
48+
countS, countT = {}, {}
49+
50+
for i in range(0, len(s)):
51+
countS[s[i]] = 1 + countS.get(s[i], 0)
52+
countT[t[i]] = 1 + countT.get(t[i], 0)
53+
54+
# To compare both the hash tables, start a loop.
55+
# If the value(freq) of a key(character) in hash table S is not equal to that in T, return False.
56+
# But what if that character is not in T at all? The comparison will throw error. So set a default value of that character to 0.
57+
# If the loop ends without returning false, that means they are anagrams, so return True
58+
59+
# for c in countS:
60+
# if countS[c] != countT.get(c, 0):
61+
# return False
62+
63+
# return True
64+
65+
return countS == countT
66+
67+
Time Complexity = O(n + m)
68+
Space Complexity = O(1) since we have at most 26 different characters
69+
70+
# Companies:
71+
# Bloomberg- 15
72+
# Google- 11
73+
# Amazon- 8
74+
# Microsoft- 5
75+
# Meta- 3
76+
# Apple- 3
77+
# Affirm- 3
78+
# Uber- 3
79+
# EPAM Systems- 2
80+
# Yelp- 2
81+
# Adobe- 12
82+
# Yahoo- 9
83+
# TCS- 5
84+
# Accenture- 5
85+
# Oracle- 3
86+
# Goldman Sachs- 3
87+
# JP Morgan- 2
88+
# Walmart Labs- 2
89+
# Tiktok- 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
3+
class Solution:
4+
def findMin(self, nums: List[int]) -> int:
5+
l, r = 0, len(nums) - 1
6+
res = nums[0]
7+
8+
while l <= r:
9+
if nums[l] <= nums[r]:
10+
res = min(res, nums[l])
11+
break
12+
13+
m = (l + r) // 2
14+
res = min(res, nums[m])
15+
16+
if nums[l] <= nums[m]:
17+
l = m + 1
18+
else:
19+
r = m - 1
20+
21+
return res

0 commit comments

Comments
 (0)