post_href
stringlengths 57
213
| python_solutions
stringlengths 71
22.3k
| slug
stringlengths 3
77
| post_title
stringlengths 1
100
| user
stringlengths 3
29
| upvotes
int64 -20
1.2k
| views
int64 0
60.9k
| problem_title
stringlengths 3
77
| number
int64 1
2.48k
| acceptance
float64 0.14
0.91
| difficulty
stringclasses 3
values | __index_level_0__
int64 0
34k
|
---|---|---|---|---|---|---|---|---|---|---|---|
https://leetcode.com/problems/longest-path-with-different-adjacent-characters/discuss/1955961/Python-Recursive-Solution-or-Graph-Tree-Hash-Map-Simple-DFS-Implementation | class Solution(object):
def longestPath(self, parent, s):
"""
:type parent: List[int]
:type s: str
:rtype: int
"""
self.child = {i:[] for i in range(len(parent))}
for i in range(1,len(parent)):
self.child[parent[i]].append(i)
self.m = 0
def ser(i,p):
if len(self.child[i])==0:
if p==s[i]:
return 0
self.m = max(self.m,1)
return 1
a = []
for j in self.child[i]:
a.append(ser(j,s[i]))
a.sort()
x = a[-1]
y = 0
if len(a)>1:
y = a[-2]
self.m = max(self.m,x+y+1)
if s[i]==p:
return 0
return x+1
ser(0,"")
return self.m | longest-path-with-different-adjacent-characters | Python Recursive Solution | Graph, Tree, Hash-Map Simple DFS Implementation | AkashHooda | 0 | 49 | longest path with different adjacent characters | 2,246 | 0.453 | Hard | 31,100 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2428232/94.58-faster-using-set-and-and-operator-in-Python | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
res = set(nums[0])
for i in range(1, len(nums)):
res &= set(nums[i])
res = list(res)
res.sort()
return res | intersection-of-multiple-arrays | 94.58% faster using set and & operator in Python | ankurbhambri | 6 | 172 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,101 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1988053/Python-2-beginner-friendly-approaches | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
res = []
concat = []
for i in range(len(nums)):
concat += nums[i]
for i in set(concat):
if concat.count(i) == len(nums):
res.append(i)
return sorted(res) | intersection-of-multiple-arrays | Python 2 beginner friendly approaches | alishak1999 | 4 | 229 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,102 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1988053/Python-2-beginner-friendly-approaches | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
res = []
nums = sorted(nums, key=len)
check = 0
for i in nums[0]:
for j in nums:
if i in j:
check += 1
if check == len(nums):
res.append(i)
check = 0
return sorted(res) | intersection-of-multiple-arrays | Python 2 beginner friendly approaches | alishak1999 | 4 | 229 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,103 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1978017/Python-Simple-Solution-using-reduce-and-set | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(reduce(lambda x,y: set(x) & set(y), nums)) | intersection-of-multiple-arrays | ✅ Python Simple Solution using reduce and set | constantine786 | 4 | 215 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,104 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2677805/Python-Solution-oror-Hashmap | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
d = {}
for i in range(len(nums)):
for j in nums[i]:
if j not in d:
d[j] = 1
else:
d[j]+=1
res = []
for k,v in d.items():
if v == len(nums):
res.append(k)
return sorted(res) | intersection-of-multiple-arrays | Python Solution || Hashmap | Graviel77 | 3 | 145 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,105 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1977013/Python-solution-3-line | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
a=set(nums[0])
inters=a.intersection(*nums)
return sorted(list(inters)) | intersection-of-multiple-arrays | Python solution 3 line | amannarayansingh10 | 3 | 114 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,106 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2234821/Python-Easy-Understanding | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
intersection = nums[0]
for i in range(1,len(nums)):
intersection = set(nums[i]) & set(intersection)
return sorted(list(intersection)) | intersection-of-multiple-arrays | Python Easy Understanding | theReal007 | 2 | 71 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,107 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2725370/Python3 | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
d = {}
for i in range(len(nums)):
for j in nums[i]:
if j not in d:
d[j] = 1
else:
d[j]+=1
res = []
for k,v in d.items():
if v == len(nums):
res.append(k)
return sorted(res) | intersection-of-multiple-arrays | Python3 | Sneh713 | 1 | 189 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,108 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2492173/97.81-faster-using-set-and-set.intersection | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
inter = set(nums[0])
for i in range(1, len(nums)):
inter = inter.intersection(set(nums[i]))
inter = sorted(list(inter))
return inter | intersection-of-multiple-arrays | 97.81% faster using set and set.intersection | aissa-laribi | 1 | 59 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,109 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2258686/Python3-O(n2)-to-O(nlogn)-countingSort-and-hashMap | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return self.solTwo(nums)
# Runtime: 95ms 64.64% || memory: 14.2mb 62.57%
# O(n^2) || O(1) you can do argure with this we have taken an array of size 1001
# but we are also returning an array, if you are taking that also as an extra space, this its O(m+n)
# where m is the number of elements present in the array, n is the elements present in the array
# otherwise you can say it O(1), if we aren't count the return 'array' as an extra space
def solOne(self, nums):
n = len(nums)
count = [0] * 1001
for i in range(len(nums)):
self.countingSort(nums[i], count)
return [num for num in range(1, 1001) if count[num] >= n]
def countingSort(self, nums, count):
for i in nums:
count[i] += 1
# O(nlogn) || O(n)
# Runtime: 128ms 26.36% || Memory: 14.4mb 6.60%
def solTwo(self, nums):
n = len(nums)
hashMap = {}
for i in range(len(nums)):
for j in range(len(nums[i])):
num = nums[i][j]
hashMap[num] = hashMap.get(num, 0) + 1
return sorted([key for key in hashMap.keys() if hashMap[key] >= n]) | intersection-of-multiple-arrays | Python3 O(n^2) to O(nlogn), countingSort and hashMap | arshergon | 1 | 89 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,110 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2159947/Python-or-Beginner-approach-using-hashmap-with-explanation | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
'''
Simple straight forward solution, Since every element is unique in sub lst, we can
use this information i.e. create a hashmap and if any element is occuring n times(n = len(nums))
it means it is occuring in all sub lists and rest elements are not occuring in all sub lists.
'''
res = []
d = {}
for num in nums:
for n in num:
d[n] = 1 + d.get(n,0)
for k,v in d.items():
if v == len(nums):
res.append(k)
return sorted(res) | intersection-of-multiple-arrays | Python | Beginner approach using hashmap with explanation | __Asrar | 1 | 84 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,111 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2829559/Python3-one-line-solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(reduce(lambda x,y: set(x) & set(y),nums)) | intersection-of-multiple-arrays | Python3 one line solution | tryit163281 | 0 | 2 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,112 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2820179/Simple-python-solution-using-counters-beats-96 | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
arr = []
res = []
for i in nums:
arr += i
count = Counter(arr)
for k,v in count.items():
if v == len(nums):
res.append(k)
res.sort()
return res | intersection-of-multiple-arrays | Simple python solution using counters beats 96% | aruj900 | 0 | 7 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,113 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2806017/Python-Simple-Python-Solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
if len(nums) == 1:
return sorted(nums[0])
result = []
array = nums[0]
for num in array:
count = 0
for rows in nums:
if num in rows:
count = count + 1
if count == len(nums):
result.append(num)
return sorted(result) | intersection-of-multiple-arrays | [ Python ] ✅✅ Simple Python Solution | ASHOK_KUMAR_MEGHVANSHI | 0 | 4 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,114 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2804746/Python-Basic-Solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
i=0
common_list=[]
if len(nums)<=1:
common_list=nums[0]
return sorted(common_list)
else:
while i<len(nums)-1:
# for i in range(len(nums)):
for j in range(len(nums[i])):
common_list = set(nums[i]).intersection(nums[i+1])
nums[i+1]=common_list
i+=1
# result = [i for i in a if i in b]
return sorted(common_list)
# print(nums[i][j]) | intersection-of-multiple-arrays | Python Basic Solution | salonipatadia | 0 | 3 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,115 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2760131/Quick-Python-solution-using-Set | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
s = set(nums[0])
for num in nums[1:]:
s = s.intersection(set(num))
return sorted(list(s)) | intersection-of-multiple-arrays | Quick Python solution using Set | Fredrick_LI | 0 | 4 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,116 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2738695/Binary-Search-or-Python-or-Similar-Questions | class Solution:
def bisect_left(self, arr, target, lo: int = 0, hi: int = None):
# hi is exclusive. The insert position can never be >= len(arr)
hi = (hi or len(arr)) - 1
if lo == hi + 1: # if array is empty
return -1
if arr[hi] < target:
return hi + 1
while lo < hi:
mid = lo + (hi - lo) // 2
if arr[mid] < target:
lo = mid + 1
else:
hi = mid
return lo
def intersection(self, nums: List[List[int]]) -> List[int]:
nums = [sorted(i) for i in nums]
ans = []
for num in nums[0]:
found = True
for row in range(len(nums)):
index = self.bisect_left(nums[row], num)
if not (index<len(nums[row]) and nums[row][index]==num):
found = False
break
if found:
ans.append(num)
return ans | intersection-of-multiple-arrays | Binary Search | Python | Similar Questions | abhira0 | 0 | 3 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,117 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2710880/Python-easy-to-understand-1Liner | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(set.intersection(*map(set,nums))) | intersection-of-multiple-arrays | Python easy to understand - 1Liner | envyTheClouds | 0 | 3 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,118 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2707940/Python-(Simple-and-Beginner-Friendly) | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
dict={}
res = []
for i in nums:
for j in i:
if j not in dict:
dict[j]=1
else:
dict[j]+=1
for i, j in dict.items():
if j == len(nums):
res.append(i)
return sorted(res) | intersection-of-multiple-arrays | Python (Simple and Beginner-Friendly) | vishvavariya | 0 | 4 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,119 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2707934/Python-(Simple-and-Beginner-Friendly) | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
if len(nums) == 1:
return sorted(nums[0])
a = nums[0]
result = set()
for i in a:
for j in range(1, len(nums)):
if i in nums[j] and i in nums[j-1]:
result.add(i)
else:
if i in result:
result.remove(i)
return sorted(set(result)) | intersection-of-multiple-arrays | Python (Simple and Beginner-Friendly) | vishvavariya | 0 | 3 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,120 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2659072/Python-Easy-Implementation | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
d={}
ans=[]
for i in nums[0]:
if i in d.keys():
continue
else:
d[i]=1
n=len(nums)
for i in range(1,len(nums)):
s=set(nums[i])
for j in s:
if j in d.keys():
d[j]+=1
else:
continue
for i in d.keys():
if d[i]==n:
ans.append(i)
ans.sort()
return ans | intersection-of-multiple-arrays | Python - Easy Implementation | utsa_gupta | 0 | 10 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,121 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2573031/python-solution-94.53-faster-using-stack | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
stack = [set(nums[0])]
for i in range(1, len(nums)):
stack.append(set(nums[i]).intersection(stack.pop()))
return sorted(list(stack.pop())) | intersection-of-multiple-arrays | python solution 94.53% faster using stack | samanehghafouri | 0 | 26 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,122 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2490840/Python-Simple-Solution-(-Brute-Force-Approach-) | class Solution:
def intersection(self, n: List[List[int]]) -> List[int]:
l,x = [] , []
for i in n:
for j in i:
if j not in l:
l.append(j)
for i in l:
for j in n:
if i not in j:
x.append(i)
break
return sorted(list(set(l) - set(x))) | intersection-of-multiple-arrays | Python Simple Solution ( Brute Force Approach ) | SouravSingh49 | 0 | 42 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,123 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2326000/One-Liner-Python-Solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(list(reduce(set.intersection, [set(i) for i in nums ]))) | intersection-of-multiple-arrays | One Liner Python Solution | murad928 | 0 | 47 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,124 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2243719/Python-Solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(reduce(lambda x, y: x & set(nums[y]), range(1, len(nums)), set(nums[0]))) | intersection-of-multiple-arrays | Python Solution | hgalytoby | 0 | 62 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,125 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2243719/Python-Solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
result = set(nums[0])
for i in range(1, len(nums)):
result &= set(nums[i])
return sorted(result) | intersection-of-multiple-arrays | Python Solution | hgalytoby | 0 | 62 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,126 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2195351/Python-Simple-Solution-or-Faster-than-99.71 | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
d = {}
ind = 0 #index of the list
for list in nums:
for item in list:
if item in d:
d[item].add(ind)
else:
d[j] = {ind}
ind += 1
l = len(nums)
ans = [i for i in d if len(d[i]) == l] #list of items that in dictionary have all indexes of lists
ans.sort()
return ans | intersection-of-multiple-arrays | Python Simple Solution | Faster than 99.71% | Bec1l | 0 | 65 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,127 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2171074/Python-oror-Simple-Solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
inter = []
count = defaultdict(int)
for i in range(len(nums)):
for j in range(len(nums[i])):
count[nums[i][j]] += 1
if count[nums[i][j]] == len(nums): inter.append(nums[i][j])
return sorted(inter) | intersection-of-multiple-arrays | Python || Simple Solution | morpheusdurden | 0 | 86 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,128 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2147169/one-line-solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(set.intersection(*map(set, nums))) | intersection-of-multiple-arrays | one line solution | writemeom | 0 | 52 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,129 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2116207/Python-Easy-to-understand-Solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
inter_dict = {}
n = len(nums)
ans = []
for i in range(n):
for key in nums[i]:
inter_dict[key] = inter_dict.get(key, 0) + 1
for key, value in inter_dict.items():
if value == n:
ans.append(key)
ans.sort()
return ans | intersection-of-multiple-arrays | ✅Python Easy-to-understand Solution | chuhonghao01 | 0 | 55 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,130 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/2033157/Python-simple-solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
ans = {x for x in range(1001)}
for i in nums:
ans = ans & set(i)
return list(sorted(ans)) | intersection-of-multiple-arrays | Python simple solution | StikS32 | 0 | 86 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,131 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1992776/Python-Elegant-Solution-Easy-To-Understand | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
m = {}
for lst in nums:
for i in lst:
if i not in m:
m[i] = 1
else:
m[i] += 1
return sorted([i for i in m if m[i] == len(nums)]) | intersection-of-multiple-arrays | Python Elegant Solution, Easy To Understand | Hejita | 0 | 68 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,132 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1984777/Python-One-Liners!-(4x) | class Solution:
def intersection(self, nums):
return sorted(reduce(lambda a,b: a & b, map(set, nums))) | intersection-of-multiple-arrays | Python - One Liners! (4x) | domthedeveloper | 0 | 89 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,133 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1984777/Python-One-Liners!-(4x) | class Solution:
def intersection(self, nums):
return sorted(reduce(operator.__and__, map(set, nums))) | intersection-of-multiple-arrays | Python - One Liners! (4x) | domthedeveloper | 0 | 89 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,134 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1984777/Python-One-Liners!-(4x) | class Solution:
def intersection(self, nums):
return sorted(reduce(set.intersection, map(set, nums))) | intersection-of-multiple-arrays | Python - One Liners! (4x) | domthedeveloper | 0 | 89 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,135 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1984777/Python-One-Liners!-(4x) | class Solution:
def intersection(self, nums):
return sorted(set.intersection(*map(set, nums))) | intersection-of-multiple-arrays | Python - One Liners! (4x) | domthedeveloper | 0 | 89 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,136 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1981315/java-python-simple-easy-small-and-the-fastest-(busket-sort) | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
numbers = [0] * 1001
for arr in nums :
for n in arr : numbers[n] += 1
ans = []
for i in range (1, 1001) :
if numbers[i] == len(nums) : ans.append(i)
return ans | intersection-of-multiple-arrays | java, python - simple, easy, small & the fastest (busket sort) | ZX007java | 0 | 24 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,137 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1980345/Python3-1-line | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(reduce(set.intersection, map(set, nums))) | intersection-of-multiple-arrays | [Python3] 1-line | ye15 | 0 | 19 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,138 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1980049/Python3-Solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
if len(nums)==1:
return sorted(nums[0])
a = set(nums[0])
for i in range(1,len(nums)):
a = a.intersection(set(nums[i]))
a = list(a)
a.sort()
return a | intersection-of-multiple-arrays | Python3 Solution | AkashHooda | 0 | 39 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,139 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1979617/python-3-oror-simple-hash-map-solution | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
counter = collections.Counter(num for arr in nums for num in arr)
n = len(nums)
return sorted(num for num, count in counter.items() if count == n) | intersection-of-multiple-arrays | python 3 || simple hash map solution | dereky4 | 0 | 40 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,140 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1979156/Python3-or-Simple-set-intersection | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
combined_sets = list(map(set,nums))
cur = combined_sets[0]
for i in range(1,len(nums)):
cur = cur.intersection(combined_sets[i])
return sorted(list(cur)) | intersection-of-multiple-arrays | Python3 | Simple set intersection | abhi-now | 0 | 25 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,141 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1978887/Python-one-liner | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(reduce(operator.__and__, map(set, nums))) | intersection-of-multiple-arrays | Python one liner | blue_sky5 | 0 | 35 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,142 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1978864/Python-1-line-using-reduce | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(reduce(lambda x, y: set(x) & set(y), nums)) | intersection-of-multiple-arrays | Python 1-line using reduce | SmittyWerbenjagermanjensen | 0 | 16 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,143 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1978407/Map-reduce-sorted-80-speed | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(reduce(lambda a, b: a & b, map(set, nums))) | intersection-of-multiple-arrays | Map, reduce, sorted, 80% speed | EvgenySH | 0 | 24 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,144 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1977083/Python3-clear-easy-to-understand-set-process | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
res = set()
for lst in nums:
if lst == nums[0]:
res = set(lst)
else:
res &= set(lst)
return sorted(list(res)) | intersection-of-multiple-arrays | Python3 clear easy to understand set process | Tallicia | 0 | 31 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,145 |
https://leetcode.com/problems/intersection-of-multiple-arrays/discuss/1977856/1-Line-Python-Solution-oror-90-Faster-oror-Memory-less-than-100 | class Solution:
def intersection(self, nums: List[List[int]]) -> List[int]:
return sorted(reduce(lambda x,y: set(x)&set(y), nums)) | intersection-of-multiple-arrays | 1-Line Python Solution || 90% Faster || Memory less than 100% | Taha-C | -1 | 14 | intersection of multiple arrays | 2,248 | 0.695 | Easy | 31,146 |
https://leetcode.com/problems/count-lattice-points-inside-a-circle/discuss/1977094/Python-Math-(Geometry)-and-Set-Solution-No-Brute-Force | class Solution:
def countLatticePoints(self, circles: List[List[int]]) -> int:
points = set()
for x, y, r in circles:
for dx in range(-r, r + 1, 1):
temp = math.floor(math.sqrt(r ** 2 - dx ** 2))
for dy in range(-temp, temp + 1):
points.add((x + dx, y + dy))
return len(points) | count-lattice-points-inside-a-circle | [Python] Math (Geometry) and Set Solution, No Brute Force | xil899 | 1 | 60 | count lattice points inside a circle | 2,249 | 0.503 | Medium | 31,147 |
https://leetcode.com/problems/count-lattice-points-inside-a-circle/discuss/2700250/Python-Simple-Solution-(distance-calculation) | class Solution:
def countLatticePoints(self, circles: List[List[int]]) -> int:
res=[]
for cirlcle in circles:
x=cirlcle[0]
y= cirlcle[1]
rad = cirlcle[2]
for x1 in range(x-rad,x+rad+1):
for y1 in range(y-rad,y+rad+1):
if((x-x1)*(x-x1)+(y-y1)*(y-y1))<=rad*rad:
res.append((x1,y1))
mylist=list(set(res))
return len(mylist) | count-lattice-points-inside-a-circle | Python Simple Solution (distance calculation) | hasan2599 | 0 | 20 | count lattice points inside a circle | 2,249 | 0.503 | Medium | 31,148 |
https://leetcode.com/problems/count-lattice-points-inside-a-circle/discuss/1986495/python-math-and-simmetry | class Solution:
def countLatticePoints(self, circles: List[List[int]]) -> int:
table = set()
for c in circles :
xl = c[0] - c[2]
xr = c[0] + c[2]
yu = c[1]
yd = c[1]
step = 1
r2 = c[2]*c[2]
for x in range (xl, xr +1) : table.add((x<<8) + yu)
while step <= c[2] :
yu += 1
yd -= 1
x = int(math.sqrt(r2 - step*step))
for X in range(c[0] - x, c[0] + x + 1) :
table.add((X<<8) + yu)
table.add((X<<8) + yd)
step += 1
return len(table) | count-lattice-points-inside-a-circle | python - math & simmetry | ZX007java | 0 | 99 | count lattice points inside a circle | 2,249 | 0.503 | Medium | 31,149 |
https://leetcode.com/problems/count-lattice-points-inside-a-circle/discuss/1981240/Three-loops-100-speed | class Solution:
def countLatticePoints(self, circles: List[List[int]]) -> int:
points = set()
for cx, cy, r in circles:
r2 = r * r
for x in range(-r, r + 1):
y_05 = int(pow(r2 - x * x, 0.5))
for y in range(-y_05, y_05 + 1):
points.add((cx + x, cy + y))
return len(points) | count-lattice-points-inside-a-circle | Three loops, 100% speed | EvgenySH | 0 | 30 | count lattice points inside a circle | 2,249 | 0.503 | Medium | 31,150 |
https://leetcode.com/problems/count-lattice-points-inside-a-circle/discuss/1980347/Python3-interval-merging | class Solution:
def countLatticePoints(self, circles: List[List[int]]) -> int:
intervals = [[] for _ in range(201)]
for x, y, r in circles:
intervals[x].append((y-r, y+r))
for dx in range(1, r+1):
dy = int(sqrt(r**2 - dx**2))
intervals[x+dx].append((y-dy, y+dy))
intervals[x-dx].append((y-dy, y+dy))
ans = 0
for interval in intervals:
if interval:
end = -inf
for i, (lo, hi) in enumerate(sorted(interval)):
if end < lo:
if i: ans += end - start + 1
start, end = lo, hi
else: end = max(end, hi)
ans += end - start + 1
return ans | count-lattice-points-inside-a-circle | [Python3] interval merging | ye15 | 0 | 13 | count lattice points inside a circle | 2,249 | 0.503 | Medium | 31,151 |
https://leetcode.com/problems/count-lattice-points-inside-a-circle/discuss/1977441/Easy-and-Fast-Python-code | class Solution:
def countLatticePoints(self, circles: List[List[int]]) -> int:
def checkInside(circle,point):
dist = sqrt((circle[0]-point[0])**2 + (circle[1]-point[1])**2)
if (dist<=circle[2]):
return True
visited = {}
for circle in circles:
r = circle[2]
for i in range(circle[0]-r,circle[0]+r+1):
for j in range(circle[1]-r,circle[1]+r+1):
if (i,j) not in visited:
if checkInside(circle,[i,j]):
visited[(i,j)] = 1
return len(visited) | count-lattice-points-inside-a-circle | Easy and Fast Python code | shvmsnju | 0 | 38 | count lattice points inside a circle | 2,249 | 0.503 | Medium | 31,152 |
https://leetcode.com/problems/count-lattice-points-inside-a-circle/discuss/1977146/Different-time-complexity-between-a**2-and-a-*-a-in-python | class Solution:
def countLatticePoints(self, circles: List[List[int]]) -> int:
result = set()
for circle in circles:
x, y, r = circle
for i in range(x - r, x + r + 1):
for j in range(y - r, y + r + 1):
if (i - x) * (i - x) + (j - y) * (j - y) <= r * r:
result.add((i, j))
return len(result) | count-lattice-points-inside-a-circle | Different time complexity between a**2 and a * a in python | Mikey98 | 0 | 38 | count lattice points inside a circle | 2,249 | 0.503 | Medium | 31,153 |
https://leetcode.com/problems/count-number-of-rectangles-containing-each-point/discuss/1980349/Python3-binary-search | class Solution:
def countRectangles(self, rectangles: List[List[int]], points: List[List[int]]) -> List[int]:
mp = defaultdict(list)
for l, h in rectangles: mp[h].append(l)
for v in mp.values(): v.sort()
ans = []
for x, y in points:
cnt = 0
for yy in range(y, 101):
if yy in mp: cnt += len(mp[yy]) - bisect_left(mp[yy], x)
ans.append(cnt)
return ans | count-number-of-rectangles-containing-each-point | [Python3] binary search | ye15 | 3 | 55 | count number of rectangles containing each point | 2,250 | 0.341 | Medium | 31,154 |
https://leetcode.com/problems/count-number-of-rectangles-containing-each-point/discuss/1978008/Python-Binary-Search-(detailed-explanation) | class Solution:
def countRectangles(self, rectangles: List[List[int]], points: List[List[int]]) -> List[int]:
maxH = 101
hToL = [[] for _ in range(maxH)]
# Create the 100 list (0 is not used)
for l, h in rectangles:
hToL[h].append(l)
# Sort the 100 list
for h in range(1, maxH):
hToL[h].sort()
res = []
for px, py in points:
count = 0
# Only search the height (y) which equals to or greater than given py
for h in range(py, maxH):
if len(hToL[h]) == 0:
continue
# Find the first index of length (x) which equals to or greater than given px in the sorted array
idx = bisect.bisect_left(hToL[h], px)
count += len(hToL[h]) - idx
res.append(count)
return res
# Time Complexity: O(100*RlogR + P*100*logR) = O(RlogR + PlogR) //if we consider 100 is a constant
# Space Complexity: O(R) | count-number-of-rectangles-containing-each-point | [Python] Binary Search (detailed explanation) | ianlai | 2 | 109 | count number of rectangles containing each point | 2,250 | 0.341 | Medium | 31,155 |
https://leetcode.com/problems/count-number-of-rectangles-containing-each-point/discuss/2244569/Python-Binary-Search-O(nlogn) | class Solution:
def countRectangles(self, rect: List[List[int]], points: List[List[int]]) -> List[int]:
hashmap = {}
for x, y in rect:
hashmap[y] = hashmap.get(y, []) + [x]
for k in hashmap.keys():
hashmap[k].sort()
res = []
for x, y in points:
ans = 0
for coord in range(y, 101):
if coord in hashmap:
val = hashmap[coord]
ans += len(val) - bisect_left(val, x)
res.append(ans)
return res | count-number-of-rectangles-containing-each-point | Python, Binary Search, O(nlogn) | mint314 | 0 | 77 | count number of rectangles containing each point | 2,250 | 0.341 | Medium | 31,156 |
https://leetcode.com/problems/count-number-of-rectangles-containing-each-point/discuss/1979546/Python3-Binary-Search-O(nlogn) | class Solution:
def countRectangles(self, rectangles: List[List[int]], points: List[List[int]]) -> List[int]:
rectangles.sort(key = lambda x : x[0])
for i in range(len(points)):
points[i] += [i]
points.sort(key = lambda x : x[0])
data_x = [x[0] for x in rectangles]
data_y = []
prev_ind = len(rectangles)
res = [0]*len(points)
for i in range(len(points)-1, -1, -1):
q_x, q_y, loc = points[i]
ind = bisect.bisect_left(data_x, q_x)
for t in range(ind, prev_ind):
bisect.insort(data_y, rectangles[t][1])
prev_ind = ind
res[loc] = len(data_y) - bisect.bisect_left(data_y, q_y)
return res | count-number-of-rectangles-containing-each-point | Python3 Binary Search O(nlogn) | xxHRxx | 0 | 31 | count number of rectangles containing each point | 2,250 | 0.341 | Medium | 31,157 |
https://leetcode.com/problems/number-of-flowers-in-full-bloom/discuss/2757459/Binary-Search-with-Explanation-Fast-and-Easy-Solution | class Solution:
def fullBloomFlowers(self, flowers: List[List[int]], persons: List[int]) -> List[int]:
start, end, res = [], [], []
for i in flowers:
start.append(i[0])
end.append(i[1])
start.sort() #bisect only works with sorted data
end.sort()
for p in persons:
num = bisect_right(start, p) - bisect_left(end, p)
res.append(num)
return res
#bisect_right(start, p) gives you the number of flowers that are in full bloom at person p.
#bisect_left(end, p) gives you number of flowers that are not in full bloom at person p.
#we have to tighten our bound to get exact number of flowers that are in bloom or not, thats why we are using right and left of bisect module. | number-of-flowers-in-full-bloom | Binary Search with Explanation - Fast and Easy Solution | user6770yv | 0 | 2 | number of flowers in full bloom | 2,251 | 0.519 | Hard | 31,158 |
https://leetcode.com/problems/number-of-flowers-in-full-bloom/discuss/2597793/Short-and-easy-solution-with-sorting-in-Python | class Solution:
def fullBloomFlowers(self, flowers: List[List[int]], persons: List[int]) -> List[int]:
all_points = sorted([(x, 0) for x in persons] + [(x, -1) for x, _ in flowers] + [(x, 1) for _, x in flowers])
results = {}
cnt = 0
for x, t in all_points:
if t == -1:
cnt += 1
elif t == 1:
cnt -= 1
else:
results[x] = cnt
return [results[p] for p in persons] | number-of-flowers-in-full-bloom | Short and easy solution with sorting in Python | metaphysicalist | 0 | 9 | number of flowers in full bloom | 2,251 | 0.519 | Hard | 31,159 |
https://leetcode.com/problems/number-of-flowers-in-full-bloom/discuss/1980350/Python3-line-sweeping-and-binary-search | class Solution:
def fullBloomFlowers(self, flowers: List[List[int]], persons: List[int]) -> List[int]:
line = []
for start, end in flowers:
line.append((start, +1))
line.append((end+1, -1))
vals = []
prefix = 0
for x, y in sorted(line):
prefix += y
vals.append((x, prefix))
ans = []
for p in persons:
i = bisect_right(vals, p, key=lambda x: x[0])
if i: ans.append(vals[i-1][1])
else: ans.append(0)
return ans | number-of-flowers-in-full-bloom | [Python3] line sweeping & binary search | ye15 | 0 | 24 | number of flowers in full bloom | 2,251 | 0.519 | Hard | 31,160 |
https://leetcode.com/problems/number-of-flowers-in-full-bloom/discuss/1978781/Python-3-Time-offsets | class Solution:
def fullBloomFlowers(self, flowers: List[List[int]], persons: List[int]) -> List[int]:
# build offset at start/end timepoints
q = defaultdict(int)
for a, b in flowers:
q[a] += 1
q[b + 1] -= 1
# record visitor coming time and index in answer array
ans = [0] * len(persons)
loc = defaultdict(set)
for i, p in enumerate(persons):
loc[p].add(i)
# sort bloom and visitor timepoints
q_k = sorted(q)
loc_k = sorted([k for k in loc if k >= q_k[0] and k <= q_k[-1]]) # arrive prior to the first bloom start and after last bloom end will end up 0
cur = 0
for k in q_k:
cur += q[k]
while loc_k and k >= loc_k[0]:
t_come = loc_k.pop(0)
for i in loc[t_come]:
ans[i] = cur - (k > t_come) * q[k] # if visit time prior to current bloom start/end time, then cancel offset
if not loc_k: return ans
return ans | number-of-flowers-in-full-bloom | [Python 3] Time offsets | chestnut890123 | 0 | 33 | number of flowers in full bloom | 2,251 | 0.519 | Hard | 31,161 |
https://leetcode.com/problems/number-of-flowers-in-full-bloom/discuss/1978698/python-solution-using-binary-search | class Solution:
def fullBloomFlowers(self, flowers: List[List[int]], persons: List[int]) -> List[int]:
start, end = [] , [] # start, end record the day that flower bloom/wither
res = []
for s, e in flowers:
start.append(s)
end.append(e)
start.sort()
end.sort()
for p in persons:
flower = bisect_right(start, p) - bisect_left(end, p) # The former will return how many flowers bloom before(include) day-p,
res.append(flower) # and the later will return how many flowers wither before(not-include) day-p,
return res | number-of-flowers-in-full-bloom | python solution using binary search | byroncharly3 | 0 | 29 | number of flowers in full bloom | 2,251 | 0.519 | Hard | 31,162 |
https://leetcode.com/problems/number-of-flowers-in-full-bloom/discuss/1977125/Python3-Sorting-according-to-event-time-O(-(M%2BN)-log-(M%2BN)-) | class Solution:
def fullBloomFlowers(self, flowers: List[List[int]], persons: List[int]) -> List[int]:
events = sorted([(start, "S", -1) for start, _ in flowers] +
[(end+1, "E", -1) for _, end in flowers] +
[(person, "Z", i) for i, person in enumerate(persons)])
sol = [0]*len(persons)
bloom = 0
for ts, evt, pidx in events:
if evt == "S":
bloom += 1
elif evt == "E":
bloom -= 1
else:
sol[pidx] = bloom
return sol | number-of-flowers-in-full-bloom | [Python3] Sorting according to event time O( (M+N) log (M+N) ) | jessee780522 | 0 | 41 | number of flowers in full bloom | 2,251 | 0.519 | Hard | 31,163 |
https://leetcode.com/problems/number-of-flowers-in-full-bloom/discuss/1977090/Prefix-Sum-%2B-Binary-Search-based-Solution | class Solution:
def create_prefix_sum(self, result):
def get_prefix_sum(arr):
n = len(arr)
prefix_sum = [0] * (n + 1)
for i in range(1, n + 1):
prefix_sum[i] = prefix_sum[i - 1] + arr[i - 1]
return prefix_sum
A = []
for r in result:
if 's' in r:
A.append(1)
else:
A.append(-1)
output = get_prefix_sum(A)
return output
def fullBloomFlowers(self, intervals: List[List[int]], persons: List[int]) -> List[int]:
result = []
sort_first = sorted(intervals, key=lambda interval: interval[0])
sort_second = sorted(intervals, key=lambda interval: interval[1])
for i in range(0, len(intervals)):
result.append(str(sort_first[i][0]) + 's')
result.append(str(sort_second[i][1]+1) + 'e')
result = sorted(result, key=lambda word:int("".join(c for c in word if c.isdigit())))
PS = self.create_prefix_sum(result)
result = [int(c.rstrip(c[-1])) for c in result]
res = []
for p in persons:
idx = bisect_right(result, p)
res.append(PS[idx])
return res | number-of-flowers-in-full-bloom | Prefix Sum + Binary Search based Solution | cppygod | 0 | 77 | number of flowers in full bloom | 2,251 | 0.519 | Hard | 31,164 |
https://leetcode.com/problems/number-of-flowers-in-full-bloom/discuss/1977053/python3-Sweep-Line | class Solution:
def fullBloomFlowers(self, flowers: List[List[int]], persons: List[int]) -> List[int]:
fakePerson = [[x, 0, i] for i, x in enumerate(persons)]
flow = []
for start, end in flowers:
flow.append([start, -float('inf')])
flow.append([end, float('inf')])
flow += fakePerson
flow.sort()
cur = 0
n = len(persons)
res = [0] * n
for item in flow:
if len(item) == 2:
if item[1] == -float('inf'):
cur += 1
else:
cur -= 1
else:
_, _, idx = item
res[idx] = cur
return res
``` | number-of-flowers-in-full-bloom | [python3] Sweep Line | icomplexray | 0 | 35 | number of flowers in full bloom | 2,251 | 0.519 | Hard | 31,165 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2076295/Easy-python-solution | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count=0
for i in words:
if (s[:len(i)]==i):
count+=1
return count | count-prefixes-of-a-given-string | Easy python solution | tusharkhanna575 | 9 | 336 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,166 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2098364/PYTHON-oror-EASY-oror-BEGINER-FRIENDLY | class Solution:
def countPrefixes(self, a: List[str], s: str) -> int:
r=0
for i in a:
if len(i)<=len(s) and i==s[:len(i)]:
r+=1
return r | count-prefixes-of-a-given-string | ✔️PYTHON || EASY || BEGINER FRIENDLY | karan_8082 | 1 | 65 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,167 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2022531/python3-One-Line-Solution | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum([1 for w in words if w == s[:len(w)]]) | count-prefixes-of-a-given-string | [python3] One-Line Solution | terrencetang | 1 | 54 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,168 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/1995485/Python-one-liner | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(w == s[:len(w)] for w in words) | count-prefixes-of-a-given-string | Python one liner | blue_sky5 | 1 | 35 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,169 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2827383/PYTHON-SOLUTION | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
st = 0
count = 0
for pre in words:
n = len(pre)
st = 0
if n==1:
if s[0] == pre:
count+=1
else:
if n<=len(s):
while (st<n):
print(pre[st],s[st])
print(count)
if pre[st]!=s[st]:
break
if st==n-1:
count+=1
st=st+1
return count | count-prefixes-of-a-given-string | PYTHON SOLUTION | ameenusyed09 | 0 | 3 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,170 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2732703/Very-easy-solution-in-python | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
res = 0
for w in words:
n = len(w)
if w == s[:n]:
res += 1
return res | count-prefixes-of-a-given-string | Very easy solution in python | ankurbhambri | 0 | 6 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,171 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2715983/PYTHON-100-EASY-TO-UNDERSTANDSIMPLECLEAN | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count = 0
for i in words:
if s[0:len(i)] == i:
count += 1
return count | count-prefixes-of-a-given-string | 🔥PYTHON 100% EASY TO UNDERSTAND/SIMPLE/CLEAN🔥 | YuviGill | 0 | 4 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,172 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2706240/Python-Easy-solution | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count=0
for i in words:
if s.startswith(i):
count+=1
return count | count-prefixes-of-a-given-string | Python Easy solution | envyTheClouds | 0 | 4 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,173 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2689600/Python3-Set-of-all-Prefixes | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
# create a set of all prefixes
prefix_set = set()
for idx in range(1, len(s)+1):
prefix_set.add(s[:idx])
# go over all of the words
return sum(word in prefix_set for word in words) | count-prefixes-of-a-given-string | [Python3] - Set of all Prefixes | Lucew | 0 | 6 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,174 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2642155/Python | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count = 0
for w in words:
if s[:len(w)] == w:
count += 1
return count | count-prefixes-of-a-given-string | Python | chakalivinith | 0 | 11 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,175 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2621783/Solution | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count = 0
for i in range(len(words)):
if words[i] in s[:len(words[i])]:
count += 1
return count | count-prefixes-of-a-given-string | Solution | fiqbal997 | 0 | 5 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,176 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2540218/Python-1-liner-with-builtins | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return len(list(filter(s.startswith, words))) | count-prefixes-of-a-given-string | [Python] - 1-liner with builtins | Lucew | 0 | 26 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,177 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2451446/Python-List-Comprehensions | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return len([i for i in words if s.startswith(i)]) | count-prefixes-of-a-given-string | Python List Comprehensions | VoidCupboard | 0 | 26 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,178 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2402405/Easy-and-clear-solution-python3 | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count=0
for word in words:
if word == s[:len(word)]:
count+=1
return count | count-prefixes-of-a-given-string | Easy and clear solution python3 | moazmar | 0 | 29 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,179 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2382758/Fast-Python-Solution-(less-memory) | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
res = 0
for i in words:
if len(i) <= len(s):
if i == s[:len(i)]:
res += 1
return res | count-prefixes-of-a-given-string | Fast Python Solution (less memory) | salsabilelgharably | 0 | 17 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,180 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2201266/Python-3-1-liner-solution | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(s[:len(c)]==c for c in words) | count-prefixes-of-a-given-string | [Python 3] 1-liner solution | s6820w | 0 | 29 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,181 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2162506/Python-or-Simple-or-Begginer-friendly-solution | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
total = 0
for i in range(len(s)+1):
if s[:i] in set(words):
total+=words.count(s[:i])
return total | count-prefixes-of-a-given-string | Python | Simple | Begginer friendly solution | manikanthgoud123 | 0 | 47 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,182 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2152557/Python-or-97-faster-and-94-memory-more-efficient-than-other-solutions | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count = 0
for word in words:
n = len(word)
if s[:n] == word:
count += 1
return count | count-prefixes-of-a-given-string | Python | 97% faster and 94% memory more efficient than other solutions | __Asrar | 0 | 53 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,183 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2107020/Python3-prefix-sum-easy-with-explanation | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
# Simple for me as problem of pefix sum
# Traverse the string s and maintain prefix and check it is present or not in words
# To deal with duplicates , maintain dictionary and track values.
count=0
ans=""
dict=Counter(words)
for i in s:
ans+=i
if ans in words:
count+=dict[ans]
return count | count-prefixes-of-a-given-string | Python3, prefix sum , easy , with explanation | Aniket_liar07 | 0 | 34 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,184 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2096283/Python3-brute-force | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count = 0
for word in words:
isHavePrefix = False
for i in range(len(word)):
if i < len(s):
if word[i] == s[i]:
isHavePrefix = True
else:
isHavePrefix = False
break
else:
isHavePrefix = False
break
if isHavePrefix:
count += 1
return count | count-prefixes-of-a-given-string | [Python3] brute force | Shiyinq | 0 | 24 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,185 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2059640/Python-easy-to-read-and-understand | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
ans = 0
for word in words:
n = len(word)
ans += 1 if word == s[:n] else 0
return ans | count-prefixes-of-a-given-string | Python easy to read and understand | sanial2001 | 0 | 26 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,186 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2044559/Fast-Python-Solution-No-Memroy | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
cnt = 0
for word in words:
l = len(word)
if word == s[:l]:
cnt += 1
return cnt | count-prefixes-of-a-given-string | Fast Python Solution, No Memroy | Hejita | 0 | 27 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,187 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2033141/Python-solution | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
ans = 0
for i in words:
if i == s[:len(i)]:
ans += 1
return ans | count-prefixes-of-a-given-string | Python solution | StikS32 | 0 | 35 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,188 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2006479/Python-without-startswith | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
def compare(word):
return s[:len(word)] == word
return sum(map(compare, words)) | count-prefixes-of-a-given-string | Python without startswith | cyqjoseph | 0 | 29 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,189 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2003795/Python-One-Liner-(x2)! | class Solution:
def countPrefixes(self, words, s):
return sum(s.find(w) == 0 for w in words) | count-prefixes-of-a-given-string | Python - One Liner (x2)! | domthedeveloper | 0 | 30 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,190 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2003795/Python-One-Liner-(x2)! | class Solution:
def countPrefixes(self, words, s):
return sum(s.startswith(w) for w in words) | count-prefixes-of-a-given-string | Python - One Liner (x2)! | domthedeveloper | 0 | 30 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,191 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/2003748/java-python-brute-force | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
answer = 0
for w in words :
if len(w) <= len(s) :
flag = True
for i in range(len(w)) :
if w[i] != s[i] :
flag = False
break
if flag : answer += 1
return answer | count-prefixes-of-a-given-string | java, python - brute force | ZX007java | 0 | 19 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,192 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/1996467/Python-easy-solution-using-startswith() | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
res = 0
for i in words:
if s.startswith(i):
res += 1
return res | count-prefixes-of-a-given-string | Python easy solution using startswith() | alishak1999 | 0 | 21 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,193 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/1996129/python-3-oror-one-line | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(s.startswith(word) for word in words) | count-prefixes-of-a-given-string | python 3 || one line | dereky4 | 0 | 20 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,194 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/1995107/Python-Simple-Solution-oror-Starts-With | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
count = 0
for i in words:
if s.startswith(i):
count += 1
return count | count-prefixes-of-a-given-string | [Python] Simple Solution || Starts With | abhijeetmallick29 | 0 | 12 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,195 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/1995059/Python-oror-100-Faster-oror-Without-Inbuilt-Function | class Solution(object):
#Function to check whether s2 is the prefix of s1
def isPrefix(self, s1, s2):
i = 0
while i < len(s2):
if s1[i] != s2[i]:
return False
i += 1
return True
def countPrefixes(self, words, s):
"""
:type words: List[str]
:type s: str
:rtype: int
"""
res = 0
n = len(s)
for word in words:
#Make sure the word length is less than length of s
if len(word) <= n and self.isPrefix(s, word):
res += 1
return res | count-prefixes-of-a-given-string | Python || 100 % Faster || Without Inbuilt Function | Sibu0811 | 0 | 26 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,196 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/1994887/Easy-Python-Solution-oror-O(n) | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
d=dict()
"""Making dictionary of words"""
for i in words:
if i not in d: """If word is not present in d"""
d[i]=1
else:
d[i]+=1 """If word is present in d increment by 1"""
c=0
for i in range(len(s)):
if d.get(s[:i])!=None: """Check if s[:i] is present as a key in d"""
c+=d[s[:i]]
""" check if whole string s is present in d and if present add its count"""
if d.get(s[:i+1])!=None:
c+=d[s[:i+1]]
return c
```
Upvote if you find this useful. | count-prefixes-of-a-given-string | Easy Python Solution || O(n) | a_dityamishra | 0 | 26 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,197 |
https://leetcode.com/problems/count-prefixes-of-a-given-string/discuss/1994748/Python-or-Easy-to-understand-or-One-liner | class Solution:
def countPrefixes(self, words: List[str], s: str) -> int:
return sum(1 for word in words if s.startswith(word)) | count-prefixes-of-a-given-string | Python | Easy to understand | One liner | parmardiwakar150 | 0 | 15 | count prefixes of a given string | 2,255 | 0.734 | Easy | 31,198 |
https://leetcode.com/problems/minimum-average-difference/discuss/2098497/PYTHON-oror-EASY-oror-BEGINER-FRIENDLY | class Solution:
def minimumAverageDifference(self, a: List[int]) -> int:
l=0
r=sum(a)
z=100001
y=0
n=len(a)
for i in range(n-1):
l+=a[i]
r-=a[i]
d=abs((l//(i+1))-(r//(n-i-1)))
if d<z:
z=d
y=i
if sum(a)//n<z:
y=n-1
return y | minimum-average-difference | ✔️PYTHON || EASY || ✔️ BEGINER FRIENDLY | karan_8082 | 1 | 75 | minimum average difference | 2,256 | 0.359 | Medium | 31,199 |
Subsets and Splits