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/number-of-ways-to-split-array/discuss/2039264/Python-O(1)-space-Beats-100
class Solution: def waysToSplitArray(self, nums: List[int]) -> int: count, left_sum, right_sum = 0, 0, sum(nums) for i in range(len(nums)-1): left_sum+=nums[i] right_sum-=nums[i] if left_sum>=right_sum: count+=1 return count
number-of-ways-to-split-array
✅ Python O(1) space Beats 100%
constantine786
0
12
number of ways to split array
2,270
0.446
Medium
31,400
https://leetcode.com/problems/number-of-ways-to-split-array/discuss/2038251/Python-3-Prefix-Sum-Easy-and-Clear-Solution
class Solution: def waysToSplitArray(self, nums: List[int]) -> int: running_sum = [] total = 0 for i, num in enumerate(nums): total += num running_sum.append(total) total = sum(nums) splits = 0 n = len(nums) for i in range(n-1): if running_sum[i] >= total-running_sum[i]: splits += 1 return splits
number-of-ways-to-split-array
[Python 3] Prefix Sum Easy and Clear Solution
hari19041
0
89
number of ways to split array
2,270
0.446
Medium
31,401
https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/2148636/Python3-greedy
class Solution: def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int: tiles.sort() ans = ii = val = 0 for i in range(len(tiles)): hi = tiles[i][0] + carpetLen - 1 while ii < len(tiles) and tiles[ii][1] <= hi: val += tiles[ii][1] - tiles[ii][0] + 1 ii += 1 partial = 0 if ii < len(tiles): partial = max(0, hi - tiles[ii][0] + 1) ans = max(ans, val + partial) val -= tiles[i][1] - tiles[i][0] + 1 return ans
maximum-white-tiles-covered-by-a-carpet
[Python3] greedy
ye15
1
105
maximum white tiles covered by a carpet
2,271
0.327
Medium
31,402
https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/2061324/Python-Prefixsum-Binary-Search
class Solution: def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int: tiles = sorted(tiles, key = lambda x : x[0]) prefix_sum = [0] res = 0 for idx, (start, end) in enumerate(tiles): cur_cover = 0 prefix_sum.append(prefix_sum[-1] + (end - start + 1)) begin = max(0, end - carpetLen + 1) l, r = -1, len(tiles) while l + 1 < r: mid = (l + r) // 2 # >> 1 if tiles[mid][0] <= begin: l = mid else: r = mid if tiles[max(0, l)][0] <= begin <= tiles[max(0, l)][1]: cur_cover += tiles[l][1] - begin + 1 cur_cover += prefix_sum[idx + 1] - prefix_sum[l + 1] res = max(res, cur_cover) return res
maximum-white-tiles-covered-by-a-carpet
Python, Prefixsum, Binary Search
yzhao156
1
73
maximum white tiles covered by a carpet
2,271
0.327
Medium
31,403
https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/2714775/Python3-or-Self-Explanatory-Code-with-Comments-or-Sliding-Window
class Solution: def maximumWhiteTiles(self, tiles: List[List[int]], carpetLength: int) -> int: n=len(tiles) tiles.sort(key=lambda x:x[0]) #sorting on starting point i,j,whiteMarbel,ans=[0]*4 while i<n: nextStartingPoint=tiles[i][0]+carpetLength #carpet will cover till nextStartingPoint-1. partial=0 while j<n and tiles[j][1]<nextStartingPoint: whiteMarbel+=tiles[j][1]-tiles[j][0]+1 #storing all whitemarbels which are covered by carpet. j+=1 if j<n: partial=max(0,nextStartingPoint-tiles[j][0]) #if current laid carpet overlap with partial interval ans=max(ans,whiteMarbel+partial) #calculating max value whiteMarbel-=tiles[i][1]-tiles[i][0]+1 #removing value at ith index as we are sliding starting point of carpet to next index i+=1 return ans
maximum-white-tiles-covered-by-a-carpet
[Python3] | Self Explanatory Code with Comments | Sliding Window
swapnilsingh421
0
8
maximum white tiles covered by a carpet
2,271
0.327
Medium
31,404
https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/2047734/java-python-sorting-and-sliding-window
class Solution: def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int: if carpetLen == 1 : return 1 tiles.sort() ans = i = j = tmp = 0 while ans != carpetLen : end = tiles[i][0] + carpetLen - 1 while j != len(tiles) and tiles[j][1] <= end: tmp += tiles[j][1] - tiles[j][0] + 1 j += 1 if j == len(tiles) : return max(ans, tmp) if end >= tiles[j][0] : ans = max(ans, tmp + min(end, tiles[j][1]) - tiles[j][0] + 1) else :ans = max(ans, tmp) if tmp == 0 : j += 1 else : tmp -= (tiles[i][1] - tiles[i][0] + 1) i += 1 return ans
maximum-white-tiles-covered-by-a-carpet
java, python - sorting & sliding window
ZX007java
0
77
maximum white tiles covered by a carpet
2,271
0.327
Medium
31,405
https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/2039156/Python-3Prefix-Sum-%2B-Two-Way-Binary-Search
class Solution: def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int: tiles.sort() starts, ends = [], [] acc = [0] for a, b in tiles: starts.append(a) ends.append(b) acc.append(acc[-1] + b - a + 1) ans = 0 for i, (a, b) in enumerate(tiles): # from start to right cover = min(tiles[-1][-1], a + carpetLen - 1) loc = bisect.bisect(starts, cover) if cover - starts[loc - 1] + 1 >= acc[loc] - acc[loc - 1]: ans = max(ans, acc[loc] - acc[i]) else: ans = max(ans, acc[loc - 1] - acc[i] + cover - starts[loc - 1] + 1) # from end to left cover2 = max(tiles[0][0], b - carpetLen + 1) loc2 = bisect.bisect_left(ends, cover2) if ends[loc2] - cover2 + 1 >= acc[loc2+1] - acc[loc2]: ans = max(ans, acc[i + 1] - acc[loc2]) else: ans = max(ans, acc[i + 1] - acc[loc2 + 1] + ends[loc2] - cover2 + 1) return ans
maximum-white-tiles-covered-by-a-carpet
[Python 3]Prefix Sum + Two Way Binary Search
chestnut890123
0
52
maximum white tiles covered by a carpet
2,271
0.327
Medium
31,406
https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/2038987/2-Python-Solutions
class Solution: def maximumWhiteTiles(self, T: List[List[int]], k: int) -> int: T.sort() l=0 ; r=0 ; tot=0 ; ans=0 while l<len(T): if r==l or T[r][0]+k>T[l][1]: tot+=T[l][1]-T[l][0]+1 ans=max(ans,tot) l+=1 else: ans=max(ans,tot+T[r][0]+k-T[l][0]) tot-=T[r][1]-T[r][0]+1 r+=1 return ans
maximum-white-tiles-covered-by-a-carpet
2 Python Solutions
Taha-C
0
61
maximum white tiles covered by a carpet
2,271
0.327
Medium
31,407
https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/2038987/2-Python-Solutions
class Solution: def maximumWhiteTiles(self, T: List[List[int]], k: int) -> int: T.sort() ; ans=0 A=list(accumulate([h-l+1 for l,h in T], initial=0)) L=[T[i][0] for i in range(len(T))] for i in range(len(L)): end=bisect_right(L,L[i]+k-1) ans=max(ans,A[end]-A[i]-(T[end-1][1]-(L[i]+k-1) if T[end-1][1]>L[i]+k-1 else 0)) return ans
maximum-white-tiles-covered-by-a-carpet
2 Python Solutions
Taha-C
0
61
maximum white tiles covered by a carpet
2,271
0.327
Medium
31,408
https://leetcode.com/problems/maximum-white-tiles-covered-by-a-carpet/discuss/2038232/Python-O(len(tiles))
class Solution: def maximumWhiteTiles(self, tiles: List[List[int]], carpetLen: int) -> int: if not carpetLen or not tiles: return 0 def get_next(itl, lf, rt): if itl < lf - 1: next_itr = lf - 1 elif itl == lf - 1: next_itr = lf elif lf <= itl < rt: next_itr = rt elif itl == rt: next_itr = rt + 1 else: next_itr = itl + 1 return next_itr tiles.sort() itl = -1 itr = 0 iwl = iwr = 0 nw = 0 max_w = 0 max_iter = tiles[-1][1] prev_itr = -1 while itr < (tiles[-1][1] + 1): itl = itr - carpetLen njump = itr - prev_itr if njump == 1: while itr > tiles[iwr][1] and iwr < len(tiles) - 1: iwr += 1 lf, rt = tiles[iwr] if lf <= itr <= rt: nw += 1 next_itr = get_next(itr, lf, rt) if itl >= 0: while itl > tiles[iwl][1] and iwl < len(tiles) - 1: iwl += 1 lf, rt = tiles[iwl] if lf <= itl <= rt: nw -= 1 next_itr2 = get_next(itl, lf, rt) + carpetLen else: next_itr2 = math.inf prev_itr = itr itr = min(next_itr, next_itr2) else: lf, rt = tiles[iwr] if lf <= prev_itr <= rt: nw += itr - prev_itr if prev_itr - carpetLen >= 0: lf, rt = tiles[iwl] if lf <= prev_itr - carpetLen <= rt: nw -= itr - prev_itr prev_itr = itr itr += 1 max_w = max(max_w, nw) return max_w
maximum-white-tiles-covered-by-a-carpet
Python O(len(tiles))
yag313
0
66
maximum white tiles covered by a carpet
2,271
0.327
Medium
31,409
https://leetcode.com/problems/substring-with-largest-variance/discuss/2148640/Python3-pairwise-prefix-sum
class Solution: def largestVariance(self, s: str) -> int: ans = 0 seen = set(s) for x in ascii_lowercase: for y in ascii_lowercase: if x != y and x in seen and y in seen: vals = [] for ch in s: if ch == x: vals.append(1) elif ch == y: vals.append(-1) cand = prefix = least = 0 ii = -1 for i, v in enumerate(vals): prefix += v if prefix < least: least = prefix ii = i ans = max(ans, min(prefix-least, i-ii-1)) return ans
substring-with-largest-variance
[Python3] pairwise prefix sum
ye15
3
620
substring with largest variance
2,272
0.374
Hard
31,410
https://leetcode.com/problems/substring-with-largest-variance/discuss/2042949/Python-3-or-Kadane's-Algorithm-Two-Pointers-or-Explanation
class Solution: def largestVariance(self, s: str) -> int: d = collections.defaultdict(list) for i, c in enumerate(s): # for each letter, create a list of its indices d[c].append(i) ans = 0 for x, chr1 in enumerate(string.ascii_lowercase): # character 1 for chr2 in string.ascii_lowercase[x+1:]: # character 2 if chr1 == chr2 or chr1 not in d or chr2 not in d: continue prefix = i = p1 = p2 = 0 hi = hi_idx = lo = lo_idx = 0 n1, n2 = len(d[chr1]), len(d[chr2]) while p1 < n1 or p2 < n2: # two pointers if p1 < n1 and p2 < n2: if d[chr1][p1] < d[chr2][p2]: prefix, p1 = prefix+1, p1+1 # count prefix else: prefix, p2 = prefix-1, p2+1 elif p1 < n1: prefix, p1 = prefix+1, p1+1 else: prefix, p2 = prefix-1, p2+1 if prefix > hi: # update high value hi, hi_idx = prefix, i if prefix < lo: # update low value lo, lo_idx = prefix, i ans = max(ans, min(prefix-lo, i-lo_idx-1)) # update ans by calculate difference, i-lo_idx-1 is to handle when only one elements are showing up ans = max(ans, min(hi-prefix, i-hi_idx-1)) i += 1 return ans
substring-with-largest-variance
Python 3 | Kadane's Algorithm, Two Pointers | Explanation
idontknoooo
3
1,100
substring with largest variance
2,272
0.374
Hard
31,411
https://leetcode.com/problems/substring-with-largest-variance/discuss/2786797/Python3-%2B-Explanation
class Solution: def largestVariance(self, s: str) -> int: # This is similar to the Kadane's algorithm, see problem 53 before attempting this one # Here we take every permutation of 2 characters in a string and then apply Kadane algo to it # Say string is 'abcdab' # From the perspective of characters a, b the string is +1, -1, +0, +0, +1, -1 # and we want to maximize this sum # note that we also want to make sure both a and b are in there, otherwise the numbers # will be incorrect. # Also, our operation of finding the sum is not commutative, so we need permutations and # not combinations. cntr = Counter(s) res = 0 for a, b in itertools.permutations(cntr, 2): a_cnt, b_cnt = cntr[a], cntr[b] var = 0; seen_a = seen_b = False for c in s: # this won't impact the variance -- so ignore if c not in (a, b): continue if var < 0: # we have more b's than a's # if no more a's left, var would ultimately be -ve -- so break if not a_cnt: break # just add the remaining a's to var if not b_cnt: res = max(res, var + a_cnt) break # we have a's and b's remaining, so restart seen_a = seen_b = False var = 0 if c == a: var += 1 a_cnt -= 1 seen_a = True if c == b: var -= 1 b_cnt -= 1 seen_b = True if seen_a and seen_b: res = max(res, var) return res
substring-with-largest-variance
Python3 + Explanation
rasnouk
1
56
substring with largest variance
2,272
0.374
Hard
31,412
https://leetcode.com/problems/substring-with-largest-variance/discuss/2044294/Python-Kadane's-Algorithm-Simple-Solution
class Solution: def largestVariance(self, s: str) -> int: counter = Counter(s) res = 0 for a, b in permutations(counter, 2): count_a, count_b = 0, 0 remain_b = counter[b] for ch in s: if ch not in {a, b}: continue if ch == a: count_a += 1 elif ch == b: count_b += 1 remain_b -= 1 # Kadane's Algorithm, modified if count_a < count_b and remain_b > 0: count_a, count_b = 0, 0 if count_b > 0: res = max(res, count_a - count_b) return res
substring-with-largest-variance
[Python] Kadane's Algorithm, Simple Solution
feifei2016
0
488
substring with largest variance
2,272
0.374
Hard
31,413
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2039752/Weird-Description
class Solution: def removeAnagrams(self, w: List[str]) -> List[str]: return [next(g) for _, g in groupby(w, sorted)]
find-resultant-array-after-removing-anagrams
Weird Description
votrubac
39
2,900
find resultant array after removing anagrams
2,273
0.583
Easy
31,414
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2039752/Weird-Description
class Solution: def removeAnagrams(self, w: List[str]) -> List[str]: return [w[i] for i in range(0, len(w)) if i == 0 or sorted(w[i]) != sorted(w[i - 1])]
find-resultant-array-after-removing-anagrams
Weird Description
votrubac
39
2,900
find resultant array after removing anagrams
2,273
0.583
Easy
31,415
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2039900/Python-3-or-Intuitive-%2B-How-to-approach-or-O(n)-without-sorting
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: res = [] anagrams = {} for i in range(len(words)): word = words[i] counter = [0]*26 for c in word: counter[ord(c)-ord('a')] += 1 if tuple(counter) not in anagrams: res.append(word) else: if anagrams[tuple(counter)] != words[i-1]: res.append(word) anagrams[tuple(counter)] = word return res
find-resultant-array-after-removing-anagrams
Python 3 | Intuitive + How to approach | O(n) without sorting
ndus
11
719
find resultant array after removing anagrams
2,273
0.583
Easy
31,416
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2039900/Python-3-or-Intuitive-%2B-How-to-approach-or-O(n)-without-sorting
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: res = [] prev = [] for i in range(len(words)): word = words[i] counter = [0]*26 for c in word: counter[ord(c)-ord('a')] += 1 if counter != prev: res.append(word) prev = counter return res
find-resultant-array-after-removing-anagrams
Python 3 | Intuitive + How to approach | O(n) without sorting
ndus
11
719
find resultant array after removing anagrams
2,273
0.583
Easy
31,417
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2056341/python-small-and-easy-to-understand-solution
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: key = "" result = [] for word in words: letters = list(word) letters.sort() new_key = "".join(letters) if new_key != key : key = new_key result.append(word) return result
find-resultant-array-after-removing-anagrams
python - small & easy to understand solution
ZX007java
3
255
find resultant array after removing anagrams
2,273
0.583
Easy
31,418
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2056341/python-small-and-easy-to-understand-solution
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: def sort_construct(word): letters = list(word) letters.sort() return "".join(letters) key = sort_construct(words[0]) result = [words[0]] for i in range(1, len(words)): new_key = sort_construct(words[i]) if new_key != key : key = new_key result.append(words[i]) return result
find-resultant-array-after-removing-anagrams
python - small & easy to understand solution
ZX007java
3
255
find resultant array after removing anagrams
2,273
0.583
Easy
31,419
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2040051/Python-Easy-Solution-with-Comments
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: if len(words) == 1: return words i = 1 while i < len(words): anagram = "".join(sorted(words[i])) # check if words[i-1] and words[i] are anagrams if anagram == "".join(sorted(words[i - 1])): # if anagrams then remove second instance from the list of words words.pop(i) else: i += 1 return words
find-resultant-array-after-removing-anagrams
Python Easy Solution with Comments
MiKueen
2
86
find resultant array after removing anagrams
2,273
0.583
Easy
31,420
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2751510/Python-counter-oror-Solution
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: res=[words[0]] for i in range(1,len(words)): mp1,mp2=Counter(words[i-1]),Counter(words[i]) if mp1!=mp2: res.append(words[i]) return res #misread easy to medium # s=[] # ans=[] # for i in words: # mp=[0]*26 # for j in i: # mp[ord(j)-ord("a")]+=1 # if str(mp) in s: # continue # else: # ans.append(i) # s.append(str(mp)) # return ans
find-resultant-array-after-removing-anagrams
Python counter || Solution
cheems_ds_side
1
98
find resultant array after removing anagrams
2,273
0.583
Easy
31,421
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2451358/Python%3A-Faster-than-upto-97-quick-hashmaplist-answer
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: #automatically get first word hashMap ={0: words[0]} for i in range(1,len(words)): notAnagram = words[i] #sort words in alphabetical order #if not the same add to hashMap if sorted(words[i]) != sorted(words[i-1]): hashMap[i] = notAnagram return list(hashMap.values())
find-resultant-array-after-removing-anagrams
Python: Faster than upto 97% quick hashmap/list answer
BasedBrenden
1
95
find resultant array after removing anagrams
2,273
0.583
Easy
31,422
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2345429/Python-or-5-lines
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: res = [words[0]] for i in range(len(words)): #check anagram if sorted(words[i-1])!=sorted(words[i]): res.append(words[i]) return res
find-resultant-array-after-removing-anagrams
Python | 5 lines
ana_2kacer
1
100
find resultant array after removing anagrams
2,273
0.583
Easy
31,423
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2054810/4-lines-solution-with-Counter
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: # `Counter` makes a letter frequency dictionary # the == operator compares the two dictionaries # if anagrams then delete the next word # otherwise, we increment the pointer pointer = 0 while pointer < len(words)-1: if ( Counter(words[pointer]) == Counter(words[pointer+1]) ): words.pop(pointer+1) else: pointer+=1 return words ```
find-resultant-array-after-removing-anagrams
4 lines solution with Counter
fhhh09
1
31
find resultant array after removing anagrams
2,273
0.583
Easy
31,424
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2848766/Counter-and-Stack-Easy-Python3
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: from collections import Counter previous_c = Counter(words[0]) stack = [words[0]] for i in range(1, len(words)): current_c = Counter(words[i]) if current_c == previous_c: continue else: previous_c = current_c stack.append(words[i]) return stack
find-resultant-array-after-removing-anagrams
Counter and Stack Easy Python3
ben_wei
0
1
find resultant array after removing anagrams
2,273
0.583
Easy
31,425
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2733306/An-Optimized-Python-Sorting-Solution
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: prevAna, result = sorted(words[0]), [words[0]] for word in words[1:]: currAna = sorted(word) if currAna != prevAna: prevAna = currAna result.append(word) return result
find-resultant-array-after-removing-anagrams
An Optimized Python Sorting Solution
kcstar
0
1
find resultant array after removing anagrams
2,273
0.583
Easy
31,426
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2714250/Literally-do-the-operation-in-description.
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: """ Literally do the operation in description. TC: O(n), SC: O(1) """ i = 1 while i < len(words): if sorted(words[i-1]) == sorted(words[i]): words.pop(i) else: i += 1 return words
find-resultant-array-after-removing-anagrams
Literally do the operation in description.
woora3
0
1
find resultant array after removing anagrams
2,273
0.583
Easy
31,427
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2697533/Simple-Python-Solution-or-Sorting
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: start=0 while start<len(words)-1: curr=list(words[start]) nxt=list(words[start+1]) curr.sort() nxt.sort() if nxt==curr: words.pop(start+1) else: start+=1 return words
find-resultant-array-after-removing-anagrams
Simple Python Solution | Sorting
Siddharth_singh
0
2
find resultant array after removing anagrams
2,273
0.583
Easy
31,428
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2675799/Simple-Python-Solution
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: frequency = dict() result = [] for word in words: frequency[word] = Counter(word) w1 = words[0] result.append(w1) for i in range(1, len(words)): w2 = words[i] if frequency[w1] != frequency[w2]: w1 = w2 result.append(w1) return result
find-resultant-array-after-removing-anagrams
Simple Python Solution
mansoorafzal
0
23
find resultant array after removing anagrams
2,273
0.583
Easy
31,429
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2649571/Python3-Just-Use-Counter
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: w = list(zip(words, [Counter(i) for i in words])) found = True while found: found = False for i in range(1,len(w)): if w[i][1] == w[i-1][1]: del w[i] found=True break return [w[i][0] for i in range(len(w))]
find-resultant-array-after-removing-anagrams
Python3 Just Use Counter
godshiva
0
8
find resultant array after removing anagrams
2,273
0.583
Easy
31,430
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2572068/3-Python-solutions....Easy-to-understand
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: i=1 while i<len(words): if (''.join(sorted(words[i])))==(''.join(sorted(words[i-1]))): words.remove(words[i]) else: i+=1 return words
find-resultant-array-after-removing-anagrams
3 Python solutions....Easy to understand
guneet100
0
43
find resultant array after removing anagrams
2,273
0.583
Easy
31,431
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2572068/3-Python-solutions....Easy-to-understand
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: i=1 while i<len(words): if sorted(words[i])==sorted(words[i-1]): words.remove(words[i]) else: i+=1 return words
find-resultant-array-after-removing-anagrams
3 Python solutions....Easy to understand
guneet100
0
43
find resultant array after removing anagrams
2,273
0.583
Easy
31,432
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2572068/3-Python-solutions....Easy-to-understand
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: i=1 while i<len(words): if Counter(words[i])==Counter(words[i-1]): words.remove(words[i]) else: i+=1 return words
find-resultant-array-after-removing-anagrams
3 Python solutions....Easy to understand
guneet100
0
43
find resultant array after removing anagrams
2,273
0.583
Easy
31,433
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2385060/Faster-than-90-easy-to-understand-Time-Complexity-O(n)
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: n = len(words) ans = [] j = 0 for i in range(n): if ("".join(sorted(words[i])) != "".join(sorted(words[j]))) or i == n-1 : ans.append(words[j]) j = i if "".join(sorted(words[n-1])) != "".join(sorted(words[n-2])): ans.append(words[n-1]) return ans
find-resultant-array-after-removing-anagrams
Faster than 90%, easy to understand, Time Complexity O(n)
Virus_003
0
71
find resultant array after removing anagrams
2,273
0.583
Easy
31,434
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2156510/bottom-up-solution
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: # iterate from the bottom up # keep track of the most recent anagram (start from last word) # create a helper function that indicates True/False if words are anagrams # iterate from the second to last word down and compare it to the current # possible anagram. if they are anagrams, pop(i + 1) and set the new # anagram to i. if they're not, just reset the new anagram # do this until i is -1 # Time O(N) Space: O(1) curr = words[-1] for i in range(len(words) - 2, -1, -1): if sorted(words[i]) == sorted(curr): words.pop(i + 1) curr = words[i] return words
find-resultant-array-after-removing-anagrams
bottom up solution
andrewnerdimo
0
28
find resultant array after removing anagrams
2,273
0.583
Easy
31,435
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2042592/Python-3-or-Easy-to-understand-%2B-short-answer-or-Using-Sorting
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: result=[] result.append(words[0]) for i in words: if sorted(i)!=sorted(result[-1]): result.append(i) return result
find-resultant-array-after-removing-anagrams
Python 3 | Easy to understand + short answer | Using Sorting
Sasuke_U
0
37
find resultant array after removing anagrams
2,273
0.583
Easy
31,436
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2040849/Python3-using-recursive
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: def check(words, anagram): if not anagram: return words else: for i in range(len(words)): if i + 1 < len(words): c1 = Counter(words[i]); c2 = Counter(words[i + 1]) if c1 == c2: words.pop(i + 1) return check(words, True) return check(words, False) return check(words, True)
find-resultant-array-after-removing-anagrams
[Python3] using recursive
Shiyinq
0
13
find resultant array after removing anagrams
2,273
0.583
Easy
31,437
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2040183/Python-Simple-O(n)-solution-Beats-~95
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: prev = Counter('0') res = [] for word in words: hashed = Counter(word) if prev != hashed: res.append(word) prev=hashed return res
find-resultant-array-after-removing-anagrams
✅ Python Simple O(n) solution - Beats ~95%
constantine786
0
31
find resultant array after removing anagrams
2,273
0.583
Easy
31,438
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2040093/Python-or-Simple-Solution-with-sorting
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: ans = [] group = {} cur = 0 for ind, word in enumerate(words): cur = ind tmp = ''.join(sorted(word)) if tmp in group and group[tmp] == cur - 1: group[tmp] = cur else: ans.append(word) group[tmp] = cur return ans
find-resultant-array-after-removing-anagrams
Python | Simple Solution with sorting
blazers08
0
16
find resultant array after removing anagrams
2,273
0.583
Easy
31,439
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2040076/Python3-1-line
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: return [w for i, w in enumerate(words) if i == 0 or sorted(words[i-1]) != sorted(w)]
find-resultant-array-after-removing-anagrams
[Python3] 1-line
ye15
0
15
find resultant array after removing anagrams
2,273
0.583
Easy
31,440
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2040076/Python3-1-line
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: return [w for i, w in enumerate(words) if i == 0 or Counter(words[i-1]) != Counter(w)]
find-resultant-array-after-removing-anagrams
[Python3] 1-line
ye15
0
15
find resultant array after removing anagrams
2,273
0.583
Easy
31,441
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2040050/Python-Solution-Using-Stack-oror-O(n)
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: st=[] for i in range(len(words)): if not st: st.append(words[i]) elif st and sorted(st[-1])==sorted(words[i]): pass else: st.append(words[i]) return st
find-resultant-array-after-removing-anagrams
Python Solution Using Stack || O(n)
a_dityamishra
0
19
find resultant array after removing anagrams
2,273
0.583
Easy
31,442
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2039989/Python-using-Counter
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: result = [words[0]] for i in range(1, len(words)): if Counter(words[i]) != Counter(result[-1]): result.append(words[i]) return result
find-resultant-array-after-removing-anagrams
Python, using Counter
blue_sky5
0
34
find resultant array after removing anagrams
2,273
0.583
Easy
31,443
https://leetcode.com/problems/find-resultant-array-after-removing-anagrams/discuss/2039881/Python-Counter
class Solution: def removeAnagrams(self, words: List[str]) -> List[str]: new = [words[0]] for i in range(1, len(words)): if Counter(words[i - 1]) == Counter(words[i]): continue else: new.append(words[i]) return new
find-resultant-array-after-removing-anagrams
Python - Counter
GigaMoksh
0
20
find resultant array after removing anagrams
2,273
0.583
Easy
31,444
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2039754/Python-Simulation-Just-sort-the-array-special
class Solution: def maxConsecutive(self, bottom: int, top: int, special: list[int]) -> int: special.sort() res = special[0] - bottom for i in range(1, len(special)): res = max(res, special[i] - special[i - 1] - 1) return max(res, top - special[-1])
maximum-consecutive-floors-without-special-floors
Python Simulation - Just sort the array special
GigaMoksh
7
326
maximum consecutive floors without special floors
2,274
0.521
Medium
31,445
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2039808/Python-Simple-Solution-or-Single-Iteration-or-Optimized-solution-or-Difference-between-floors
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: c = 0 special.sort() c = special[0]-bottom bottom = special[0] for i in range(1,len(special)): if special[i]-bottom>1: c = max(c,special[i]-bottom-1) bottom = special[i] if top-bottom>1: c = max(c,top-bottom) return c
maximum-consecutive-floors-without-special-floors
Python Simple Solution | Single Iteration | Optimized solution | Difference between floors
AkashHooda
2
91
maximum consecutive floors without special floors
2,274
0.521
Medium
31,446
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2098273/JAVA-oror-PYTHON-oror-100-oror-EASY
class Solution: def maxConsecutive(self, x: int, y: int, a: List[int]) -> int: a.sort() r=max(a[0]-x,y-a[-1]) for i in range(len(a)-1): a[i]=a[i+1]-a[i] a[-1]=0 r=max(max(a)-1,r) return r
maximum-consecutive-floors-without-special-floors
✔️JAVA || PYTHON ||✔️ 100% || EASY
karan_8082
1
66
maximum consecutive floors without special floors
2,274
0.521
Medium
31,447
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2040148/Python-Simple-Solution-Beats-~95-(with-explanation)
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: special.sort() special_floors=[bottom-1] + special + [top+1] res = 0 for i in range(1, len(special_floors)): res = max(res, special_floors[i] - special_floors[i-1] - 1) return res
maximum-consecutive-floors-without-special-floors
Python Simple Solution Beats ~95% (with explanation)
constantine786
1
111
maximum consecutive floors without special floors
2,274
0.521
Medium
31,448
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2040058/Python-Solution-oror-Easy-To-Understand
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: c=0 l=[] special.sort() a=special[0]-bottom b=top-special[-1] for i in range(len(special)-1): l.append(special[i+1]-special[i]-1) if len(l)>=1: c=max(l) return max(a,b,c)
maximum-consecutive-floors-without-special-floors
Python Solution || Easy To Understand
a_dityamishra
1
25
maximum consecutive floors without special floors
2,274
0.521
Medium
31,449
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2317468/Python3-Using-Sort
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: """ # TLE special.sort() spec = 0 seq = max_seq = 0 for floor in range(bottom, top+1): if spec < len(special) and floor == special[spec]: max_seq = max(max_seq, seq) seq = 0 spec += 1 continue seq += 1 return max(max_seq, seq) """ special.sort() i = 0 max_seq = 0 while i + 1 < len(special): max_seq = max(max_seq, special[i+1] - special[i] - 1) i += 1 max_seq = max(max_seq, special[0] - bottom) max_seq = max(max_seq, top - special[-1]) return max_seq
maximum-consecutive-floors-without-special-floors
[Python3] Using Sort
Gp05
0
9
maximum consecutive floors without special floors
2,274
0.521
Medium
31,450
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2289973/Python3-Sorting-approach
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: special.sort() max_count = special[0] - bottom for idx in range(1, len(special)): max_count = max(max_count, special[idx] - special[idx-1] - 1) max_count = max(max_count, (top - special[-1])) return max_count
maximum-consecutive-floors-without-special-floors
Python3 Sorting approach
yashchandani98
0
11
maximum consecutive floors without special floors
2,274
0.521
Medium
31,451
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2059681/python-java-sort-%3A-small-and-easy-to-understand
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: special.sort() answer = special[0] - bottom + 1 for floor in special : answer = max(answer, floor - bottom) bottom = floor return max(answer, top - special[-1] + 1) - 1
maximum-consecutive-floors-without-special-floors
python, java - sort : small & easy to understand
ZX007java
0
37
maximum consecutive floors without special floors
2,274
0.521
Medium
31,452
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2044776/Sort-zip-max-85-speed
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: if len(special) == 1: return max(special[0] - bottom, top - special[0]) special.sort() return (max(max(b - a for a, b in zip(special, special[1:])) - 1, special[0] - bottom, top - special[-1]))
maximum-consecutive-floors-without-special-floors
Sort, zip, max, 85% speed
EvgenySH
0
16
maximum consecutive floors without special floors
2,274
0.521
Medium
31,453
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2041138/python-oror-sorting-oror-Easy-oror-Comments
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: maxi=0 # consider the lowermost possible values special.sort() a=special[0]-bottom # consider the uppermost possible values b=top-special[-1] # when both the specials floor lies in b/w top and bottom , we'd consider # case when diff is greater than 1 and count it as 1 less. for i in range(1,len(special)): c=special[i]-special[i-1] if c>1: maxi=max(maxi,c-1) return max(a,b,maxi)
maximum-consecutive-floors-without-special-floors
python || sorting || Easy || Comments
Aniket_liar07
0
8
maximum consecutive floors without special floors
2,274
0.521
Medium
31,454
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2040688/Python-oror-Sort-the-array
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: mx=0 special=sorted(special) for i in range(1,len(special)): mx=max(special[i]-special[i-1],mx) return max(mx-1,special[0]-bottom,top-special[-1])
maximum-consecutive-floors-without-special-floors
Python || Sort the array
aditya1292
0
15
maximum consecutive floors without special floors
2,274
0.521
Medium
31,455
https://leetcode.com/problems/maximum-consecutive-floors-without-special-floors/discuss/2040100/Python3-2-line
class Solution: def maxConsecutive(self, bottom: int, top: int, special: List[int]) -> int: aug = [bottom-1] + sorted(special) + [top+1] return max(aug[i]-aug[i-1]-1 for i in range(1, len(aug)))
maximum-consecutive-floors-without-special-floors
[Python3] 2-line
ye15
0
14
maximum consecutive floors without special floors
2,274
0.521
Medium
31,456
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2039717/Check-Each-Bit
class Solution: def largestCombination(self, candidates: List[int]) -> int: return max(sum(n &amp; (1 << i) > 0 for n in candidates) for i in range(0, 24))
largest-combination-with-bitwise-and-greater-than-zero
Check Each Bit
votrubac
55
3,600
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,457
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2039741/Python-Count-the-Number-of-Ones-at-Each-Bit-Clean-and-Concise
class Solution: def largestCombination(self, candidates: List[int]) -> int: s = [0 for _ in range(30)] for c in candidates: b = bin(c)[2:][::-1] for i, d in enumerate(b): if d == '1': s[i] += 1 return max(s)
largest-combination-with-bitwise-and-greater-than-zero
[Python] Count the Number of Ones at Each Bit, Clean & Concise
xil899
2
109
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,458
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2040092/Python-Check-Each-Bit-O(30*N)
class Solution: def largestCombination(self, candidates: List[int]) -> int: arr = [0]*32 for c in candidates: for i in range(0, 30): if c &amp; (1<<i) : arr[i] += 1 return max(arr)
largest-combination-with-bitwise-and-greater-than-zero
[Python] Check Each Bit O(30*N)
akwal
1
33
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,459
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2039878/Python-Efficient-Solution-or-O(30*N)-solution-or-Easy-to-understand
class Solution: def largestCombination(self, candidates: List[int]) -> int: d = {} for i in range(32): d[i] = 0 for a in candidates: x = bin(a)[2:][::-1] for j in range(len(x)): if x[j]=='1': d[j]+=1 return max(d.values())
largest-combination-with-bitwise-and-greater-than-zero
Python Efficient Solution | O(30*N) solution | Easy to understand
AkashHooda
1
47
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,460
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2788458/Python3-or-Bit-manipulation
class Solution: def largestCombination(self, A: List[int]) -> int: n = len(A) ans= 0 for i in range(32): maxCol = 0 for j in range(n): if (A[j]>>i) &amp; 1 == 1: maxCol += 1 ans = max(ans,maxCol) return ans
largest-combination-with-bitwise-and-greater-than-zero
[Python3] | Bit - manipulation
swapnilsingh421
0
8
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,461
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2087933/Python3-or-Simple-Approach
class Solution: def largestCombination(self, candidates: List[int]) -> int: ans = 0 for i in range(0,32): temp = 0 for candidate in candidates: temp += ((1<<i)&amp;(candidate))>>i ans = max(temp, ans) return ans
largest-combination-with-bitwise-and-greater-than-zero
Python3 | Simple Approach
goyaljatin9856
0
61
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,462
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2069487/java-c%2B%2B-python-bit-manipulation
class Solution: def largestCombination(self, candidates: List[int]) -> int: bits = [0]*32 for x in candidates : i = 0 while x != 0 : bits[i] += x&amp;1 i +=1 x >>= 1 ans = 0 for b in bits : ans = max(ans, b) return ans
largest-combination-with-bitwise-and-greater-than-zero
java, c++, python - bit manipulation
ZX007java
0
84
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,463
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2045623/Binary-string-of-each-number-100-speed
class Solution: def largestCombination(self, candidates: List[int]) -> int: bit_counts = [0] * 24 for n in candidates: for i, digit in enumerate(bin(n)[2:][::-1]): if digit == "1": bit_counts[i] += 1 return max(bit_counts)
largest-combination-with-bitwise-and-greater-than-zero
Binary string of each number, 100% speed
EvgenySH
0
24
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,464
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2043970/bitwise-oror-python-oror-bit-masking-oror-easy
class Solution: def largestCombination(self, candidates: List[int]) -> int: res=0 # Logic is simple and operation is !=0 only when all the ith bit of a no. is set for each candidates for i in range(32): count=0 # create mask at ith bit mask=1<<i # check for how many candidate '^' is not zero at particular bit , increase that count for val in candidates: if val&amp;mask != 0: count+=1 # for each bit count max no. of candidates whose '^' is not zero res=max(res,count) # Finally return max. candidates for particular bit return res
largest-combination-with-bitwise-and-greater-than-zero
bitwise || python || bit-masking || easy
Aniket_liar07
0
8
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,465
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2040115/Python3-1-line
class Solution: def largestCombination(self, candidates: List[int]) -> int: return max(sum(bool(x &amp; 1<<i) for x in candidates) for i in range(24))
largest-combination-with-bitwise-and-greater-than-zero
[Python3] 1-line
ye15
0
11
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,466
https://leetcode.com/problems/largest-combination-with-bitwise-and-greater-than-zero/discuss/2039860/Python3-Easy-to-understand-Explained
class Solution: def largestCombination(self, candidates: List[int]) -> int: lst, n = [], len(str(bin(max(candidates)))[2:]) for x in candidates: lst.append(str(bin(x))[2:].zfill(n)) counter = Counter() for x in lst: for i, y in enumerate(x): if y == "1": counter[i] += 1 return max(counter.values())
largest-combination-with-bitwise-and-greater-than-zero
Python3 - Easy to understand - Explained
WoodlandXander
0
40
largest combination with bitwise and greater than zero
2,275
0.724
Medium
31,467
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2061930/Simple-Python-Solution-or-Easy-to-Understand-or-Two-Liner-Solution-or-O(N)-Solution
class Solution: def percentageLetter(self, s: str, letter: str) -> int: a = s.count(letter) return (a*100)//len(s)
percentage-of-letter-in-string
Simple Python Solution | Easy to Understand | Two Liner Solution | O(N) Solution
AkashHooda
2
63
percentage of letter in string
2,278
0.741
Easy
31,468
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2710506/Python-ororO(N)-Runtime-50-ms-Beats-56.36-Memory-13.9-MB-Beats-10.66
class Solution: def percentageLetter(self, s: str, letter: str) -> int: c=0 for i in s: if i==letter: c+=1 n=len(s) return int(c/n*100)
percentage-of-letter-in-string
[Python ||O(N)] Runtime 50 ms Beats 56.36% Memory 13.9 MB Beats 10.66%
Sneh713
1
47
percentage of letter in string
2,278
0.741
Easy
31,469
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2061914/Python-Easy-Solution
class Solution: def percentageLetter(self, s: str, letter: str) -> int: cnt = 0 for char in s: if char == letter: cnt += 1 res = math.floor((cnt / len(s)) * 100) return res
percentage-of-letter-in-string
Python Easy Solution
MiKueen
1
40
percentage of letter in string
2,278
0.741
Easy
31,470
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2848335/Python-1-liner-beats-99.91
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return int(s.count(letter)*100/len(s))
percentage-of-letter-in-string
Python 1 liner, beats 99.91%
Spex924
0
2
percentage of letter in string
2,278
0.741
Easy
31,471
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2811204/Python-Solution-EASY-oror-EASY-TO-UNDERSTAND
class Solution: def percentageLetter(self, s: str, letter: str) -> int: c=0 for i in s: if i==letter: c+=1 a=(c/len(s))*100 return int(a)
percentage-of-letter-in-string
Python Solution - EASY || EASY TO UNDERSTAND
T1n1_B0x1
0
6
percentage of letter in string
2,278
0.741
Easy
31,472
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2771174/Python-or-LeetCode-or-2278.-Percentage-of-Letter-in-String
class Solution: def percentageLetter(self, s: str, letter: str) -> int: x = 0 for i in s: if i == letter: x += 1 return (100 * x) // len(s)
percentage-of-letter-in-string
Python | LeetCode | 2278. Percentage of Letter in String
UzbekDasturchisiman
0
5
percentage of letter in string
2,278
0.741
Easy
31,473
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2771174/Python-or-LeetCode-or-2278.-Percentage-of-Letter-in-String
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return (100 * s.count(letter)) // len(s)
percentage-of-letter-in-string
Python | LeetCode | 2278. Percentage of Letter in String
UzbekDasturchisiman
0
5
percentage of letter in string
2,278
0.741
Easy
31,474
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2711068/100-EASY-TO-UNDERSTANDSIMPLECLEAN
class Solution: def percentageLetter(self, s: str, letter: str) -> int: count = 0 for c in s: if c == letter: count += 1 return int(count/len(s)*100)
percentage-of-letter-in-string
🔥100% EASY TO UNDERSTAND/SIMPLE/CLEAN🔥
YuviGill
0
5
percentage of letter in string
2,278
0.741
Easy
31,475
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2553429/Python-Simple-Python-Solution
class Solution: def percentageLetter(self, s: str, letter: str) -> int: frequency_count = s.count(letter) result = (frequency_count * 100) // len(s) return result
percentage-of-letter-in-string
[ Python ] ✅✅ Simple Python Solution 🥳✌👍
ASHOK_KUMAR_MEGHVANSHI
0
10
percentage of letter in string
2,278
0.741
Easy
31,476
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2519134/Simple-Python-solution
class Solution: def percentageLetter(self, s: str, letter: str) -> int: n = len(s) count = 0 for i in s: if i == letter: count += 1 return floor((count/n)*100)
percentage-of-letter-in-string
Simple Python solution
aruj900
0
18
percentage of letter in string
2,278
0.741
Easy
31,477
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2507980/Python3-Easy-One-liner-str.count()-method-%2B-division
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return 100 * s.count(letter) // len(s)
percentage-of-letter-in-string
[Python3] Easy One-liner str.count() method + // division
ivnvalex
0
9
percentage of letter in string
2,278
0.741
Easy
31,478
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2451450/Python-simple-solution
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return round((s.count(letter) / len(s)) * 100)
percentage-of-letter-in-string
Python simple solution
VoidCupboard
0
15
percentage of letter in string
2,278
0.741
Easy
31,479
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2360497/29-ms-faster-than-95.49
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return int(s.count(letter)/len(s)*100)
percentage-of-letter-in-string
29 ms, faster than 95.49%
siktorovich
0
21
percentage of letter in string
2,278
0.741
Easy
31,480
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2165103/Percentage-of-Letter-in-String-or-Python-One-Line-or-Easy-way
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return int(Counter(s)[letter] / len(s) * 100)
percentage-of-letter-in-string
Percentage of Letter in String | Python One-Line | Easy way
YangJenHao
0
15
percentage of letter in string
2,278
0.741
Easy
31,481
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2152532/Python-or-Simple-and-clean-solution
class Solution: def percentageLetter(self, s: str, letter: str) -> int: count = 0 n = len(s) for c in s: if c == letter: count += 1 # print(count,n) return int((count / n) * 100)
percentage-of-letter-in-string
Python | Simple and clean solution
__Asrar
0
39
percentage of letter in string
2,278
0.741
Easy
31,482
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2135380/Python-oror-Straight-Forward
class Solution: def percentageLetter(self, s: str, letter: str) -> int: count = 0 for l in s: if l == letter: count += 1 ans = (count / len(s))*100 return int(ans)
percentage-of-letter-in-string
Python || Straight Forward
morpheusdurden
0
24
percentage of letter in string
2,278
0.741
Easy
31,483
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2116183/Python-Easy-to-understand-Solution
class Solution: def percentageLetter(self, s: str, letter: str) -> int: m = s.count(letter) n = len(s) x = int(100 * m / n) return x
percentage-of-letter-in-string
✅Python Easy-to-understand Solution
chuhonghao01
0
14
percentage of letter in string
2,278
0.741
Easy
31,484
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2094854/Python-one-liner-faster-than-95
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return int((s.count(letter) / len(s)) * 100)
percentage-of-letter-in-string
Python one liner faster than 95%
alishak1999
0
21
percentage of letter in string
2,278
0.741
Easy
31,485
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2089121/Python-Easy-solution-with-complexities
class Solution: def percentageLetter(self, s: str, letter: str) -> int: count = 0 for i in s: if i == letter: count = count + 1 answer = ( count / len (s) ) * 100 return int(answer) # time O(N) # space O(1)
percentage-of-letter-in-string
[Python] Easy solution with complexities
mananiac
0
19
percentage of letter in string
2,278
0.741
Easy
31,486
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2076313/Python-oneliner
class Solution: def percentageLetter(self, s: str, letter: str) -> int: from math import floor return floor(s.count(letter)/len(s)*100)
percentage-of-letter-in-string
Python oneliner
StikS32
0
17
percentage of letter in string
2,278
0.741
Easy
31,487
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2070620/Python-One-Line
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return s.count(letter) * 100 // len(s)
percentage-of-letter-in-string
Python One Line
Hejita
0
28
percentage of letter in string
2,278
0.741
Easy
31,488
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2068286/Python3-1-line
class Solution: def percentageLetter(self, s: str, letter: str) -> int: return 100*s.count(letter)//len(s)
percentage-of-letter-in-string
[Python3] 1-line
ye15
0
10
percentage of letter in string
2,278
0.741
Easy
31,489
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2065988/java-python-counter
class Solution: def percentageLetter(self, s: str, letter: str) -> int: counter = 0 for i in range(len(s)): if s[i] == letter : counter += 1 return counter*100//len(s)
percentage-of-letter-in-string
java, python - counter
ZX007java
0
24
percentage of letter in string
2,278
0.741
Easy
31,490
https://leetcode.com/problems/percentage-of-letter-in-string/discuss/2062470/Simple-Python-Solution
class Solution: def percentageLetter(self, s: str, letter: str) -> int: c=0 for i in letter: if i in s: c+=s.count(i) return c*100//len(s)
percentage-of-letter-in-string
Simple Python Solution
a_dityamishra
0
12
percentage of letter in string
2,278
0.741
Easy
31,491
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2062186/Python-Easy-Solution
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: remaining = [0] * len(capacity) res = 0 for i in range(len(capacity)): remaining[i] = capacity[i] - rocks[i] remaining.sort() for i in range(len(remaining)): if remaining[i] > additionalRocks: break additionalRocks -= remaining[i] res += 1 return res
maximum-bags-with-full-capacity-of-rocks
Python Easy Solution
MiKueen
1
28
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,492
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2061885/Python-Simple-Solution-or-Easy-to-Understand-or-O(NLogN)
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: l = [] c = 0 for i in range(len(capacity)): if capacity[i]-rocks[i]: l.append(capacity[i]-rocks[i]) else: c+=1 l.sort() for i in range(len(l)): a = l[i] if a<=additionalRocks: c+=1 additionalRocks-=a else: break return c
maximum-bags-with-full-capacity-of-rocks
Python Simple Solution | Easy to Understand | O(NLogN)
AkashHooda
1
50
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,493
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2061860/Python-or-Sort-on-basis-of-difference
class Solution: def maximumBags(self, c: List[int], r: List[int], a: int) -> int: n = len(c) new = [[c[i], r[i]] for i in range(n)] new.sort(key=lambda x: x[0] - x[1]) count = 0 for i in range(n): if new[i][0] > new[i][1]: x = new[i][0] - new[i][1] if a < x: continue else: count += 1 a -= x else: count += 1 return count
maximum-bags-with-full-capacity-of-rocks
Python | Sort on basis of difference
GigaMoksh
1
9
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,494
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2824614/Python-greedy-solution
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: empty = [] for i in range(len(rocks)): empty.append(capacity[i]-rocks[i]) empty.sort() bags = 0 for i in range(len(empty)): additionalRocks -= empty[i] if additionalRocks < 0: return bags bags += 1 return bags
maximum-bags-with-full-capacity-of-rocks
Python greedy solution
ankurkumarpankaj
0
1
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,495
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2806309/Understanding-Solution-(Python3)
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: n = len(capacity) if len(capacity) == len(rocks) : arr = {} for i in range(n): arr[i] = capacity[i]-rocks[i] arr = sorted(arr.values()) val = additionalRocks answer = 0 for j in range(n): if val >= arr[j]: val = val - arr[j] answer = answer + 1 return answer
maximum-bags-with-full-capacity-of-rocks
Understanding Solution (Python3)
Kasun98
0
1
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,496
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2688359/Python-or-Greedy-%2B-Min-heap-or-Beats-95
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additional_rocks: int) -> int: rocks_needed = [c - r for c, r in zip(capacity, rocks)] heapq.heapify(rocks_needed) bags = 0 while rocks_needed: r = heapq.heappop(rocks_needed) if additional_rocks >= r: bags += 1 additional_rocks -= r if additional_rocks == 0: break else: # We cannot cover even the minimum number of rocks # required! break return bags
maximum-bags-with-full-capacity-of-rocks
Python | Greedy + Min-heap | Beats 95%
on_danse_encore_on_rit_encore
0
4
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,497
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2248139/Python-oror-Easy-Solution
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: difflist = [] fullcapacity = 0 for i in range(0,len(rocks)): difflist.append(capacity[i] - rocks[i]) difflist.sort() for i in range(len(difflist)): if difflist[i] == 0: fullcapacity += 1 else: if additionalRocks >= difflist[i]: additionalRocks -= difflist[i] fullcapacity += 1 return fullcapacity
maximum-bags-with-full-capacity-of-rocks
Python || Easy Solution
dyforge
0
15
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,498
https://leetcode.com/problems/maximum-bags-with-full-capacity-of-rocks/discuss/2221329/Intuitive-Approach-and-Solution
class Solution: def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int: n = len(capacity) maximumBags = 0 for i in range(n): if capacity[i] - rocks[i] == 0: maximumBags += 1 difference = sorted([capacity[i] - rocks[i] for i in range(n)]) for diff in difference: if diff != 0: if additionalRocks >= diff: additionalRocks -= diff maximumBags += 1 else: break return maximumBags
maximum-bags-with-full-capacity-of-rocks
Intuitive Approach and Solution
Vaibhav7860
0
7
maximum bags with full capacity of rocks
2,279
0.626
Medium
31,499