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/minimum-deletions-to-make-character-frequencies-unique/discuss/2209096/Boom!!-Python-makes-it-easy. | class Solution:
def minDeletions(self, s: str) -> int:
deleted_elements = 0
counter = Counter(s)
freq_values = sorted(counter.values())[::-1]
for index in range(len(freq_values)):
while freq_values[index] != 0 and freq_values[index] in freq_values[:index] + freq_values[in... | minimum-deletions-to-make-character-frequencies-unique | Boom!! - Python makes it easy. | yours-truly-rshi | 2 | 127 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,900 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2207623/Python-Simple-Python-Solution-Using-HashMap-oror-Dictionary | class Solution:
def minDeletions(self, s: str) -> int:
d = {}
result = 0
visted = []
for char in s:
if char not in d:
d[char] = 1
else:
d[char] = d[char] + 1
d = dict(sorted(d.items(), key = lambda x : x[1]))
for i in d:
if d[i] not in visted:
visted.append(d[i])
else:
... | minimum-deletions-to-make-character-frequencies-unique | [ Python ] β
β
Simple Python Solution Using HashMap || Dictionary π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 2 | 130 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,901 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/1430139/Python3-or-O(n)-Time-or-O(1)-space-for-larger-n | class Solution:
def minDeletions(self, s: str) -> int:
count=[0 for i in range(26)]
for i in s: count[ord(i)-ord('a')]+=1
count=[i for i in count if i>0]
count.sort(reverse=True)
mx = max(count)
values=0
mn=count[0]
for i in range(1,len(count)):
... | minimum-deletions-to-make-character-frequencies-unique | Python3 | O(n) Time | O(1) space for larger n | HemantRana | 2 | 286 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,902 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2208062/Python-Simple-Python-Solution-Using-HashMap-oror-Dictionary | class Solution:
def minDeletions(self, s: str) -> int:
d = {}
result = 0
visted = []
for char in s:
if char not in d:
d[char] = 1
else:
d[char] = d[char] + 1
d = dict(sorted(d.items(), key = lambda x : x[1]))
for i in d:
if d[i] not in visted:
visted.append(d[i])
else:
... | minimum-deletions-to-make-character-frequencies-unique | [ Python ] β
β
Simple Python Solution Using HashMap || Dictionary π₯³βπ | ASHOK_KUMAR_MEGHVANSHI | 1 | 101 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,903 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2207450/Python3-or-easy-or-explained-or-easy-to-understand-or-Unique-Occurance | class Solution:
def minDeletions(self, s: str) -> int:
lst=[]
for e in set(s):
lst.append(s.count(e)) # append the count of each char
lst.sort(reverse=True) # sort it in reverse order
n = len(lst)
ans = 0
for i ... | minimum-deletions-to-make-character-frequencies-unique | Python3 | easy | explained | easy to understand | Unique Occurance | H-R-S | 1 | 33 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,904 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2190985/Python3-Explanation-with-comments-simple-solution | class Solution:
def minDeletions(self, s: str) -> int:
## RC ##
## APPROACH : HASHMAP ##
## LOGIC ##
## Lets take an example freq {3,3,3,4,4,4} ##
## 1. We can establish that, distinct freq we can have are {1, 2, 3, 4} ##
## 2. So, obvisouly we add highest freq value ... | minimum-deletions-to-make-character-frequencies-unique | [Python3] Explanation with comments, simple solution | 101leetcode | 1 | 40 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,905 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/1075874/Python-simple-greedy-using-Counter | class Solution:
def minDeletions(self, s: str) -> int:
#listing element quantities: example "ceabaacb" -> [2,1,3,2]
s = list(collections.Counter(s).values())
freq, count = set(), 0
for i in s:
while i in freq:
i -= 1
count += 1
... | minimum-deletions-to-make-character-frequencies-unique | Python simple greedy using Counter | Onlycst | 1 | 292 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,906 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2814974/Python-Recursive-Solution | class Solution:
def minDeletions(self, s: str) -> int:
from collections import Counter
c = Counter(s)
res = []
global cnt
cnt = 0
def foo(n):
global cnt
if n not in res:
res.append(n)
else:
cnt += 1... | minimum-deletions-to-make-character-frequencies-unique | Python Recursive Solution | fatin_istiaq | 0 | 1 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,907 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2555218/Python-Solution-or-O(N2)-or-Brute-Force | class Solution:
def minDeletions(self, s: str) -> int:
freq = {}
for ch in s:
freq[ch] = 1 + freq.get(ch, 0)
vals = list(freq.values())
vals.sort()
ans = 0
if len(vals) > 1:
for i in range(1, len(vals)):
if vals[i] == ... | minimum-deletions-to-make-character-frequencies-unique | Python Solution | O(N^2) | Brute Force | dos_77 | 0 | 31 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,908 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2268268/Python3-Simple-Solution | class Solution:
def minDeletions(self, s: str) -> int:
maps = {}
for x in s :
if x not in maps :
maps[x] = 0
maps[x] += 1
maps = [ [v,k] for k,v in maps.items()]
maps.sort()
counter = 0
x = 1
... | minimum-deletions-to-make-character-frequencies-unique | Python3 Simple Solution | jasoriasaksham01 | 0 | 84 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,909 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2210463/Minimum-Deletions-to-Make-Character-Frequencies-Unique-or-Easy-python-solution-using-set | class Solution:
def minDeletions(self, s: str) -> int:
deletions = 0
char_counts = collections.Counter(s)
frequency_set = set()
for char,count in char_counts.items():
while count > 0 and count in frequency_set:
count -= 1
deletions += 1
... | minimum-deletions-to-make-character-frequencies-unique | Minimum Deletions to Make Character Frequencies Unique | Easy python solution using set | nishanrahman1994 | 0 | 6 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,910 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2210064/O(N)-Solution-in-Python | class Solution:
def minDeletions(self, s: str) -> int:
d = {}
for c in s:
d[c] = d.get(c, 0) + 1 # Calculate indices
h = [[] for i in range(len(s)+1)] # make an array for indices.
for k in d:
val = d[k] # frequency
h[val].append(k) # add c... | minimum-deletions-to-make-character-frequencies-unique | O(N) Solution in Python | saqibmubarak | 0 | 10 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,911 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2209893/Python-solution | class Solution:
def minDeletions(self, s: str) -> int:
char_dict = {}
count_set = set()
final_count = 0
for i in s:
if i in char_dict:
char_dict[i] += 1
else:
char_dict[i] = 1
sorted(char_dict,key=char_dict.get,reverse=True)
char_dict = {k:v for k,v in cha... | minimum-deletions-to-make-character-frequencies-unique | Python solution | NiketaM | 0 | 6 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,912 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2209526/Pythonoror-Easy-to-understand-with-explaination-ororGreedy-ApproachororHash-MapororNO-SORTING | class Solution:
def minDeletions(self, s: str) -> int:
freq,seen_freq,ans={},[],0
for ch in s:
#storing character and it's frequency in a hash map
freq[ch]=freq.get(ch,0)+1
for character,frequency in freq.items():
#if some previous character had the same ... | minimum-deletions-to-make-character-frequencies-unique | Python|| Easy to understand with explaination β
||Greedy Approach||Hash Map||NO SORTING | 695roshan | 0 | 7 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,913 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2208463/Python3-oror-hashMap | class Solution:
def minDeletions(self, s: str) -> int:
hashMap, res = {}, 0
for c in s:
if c in hashMap.keys():
hashMap[c] += 1
else:
hashMap[c] = 1
f_arr = list(hashMap.values())
f_arr.sort(reverse=True)
f = f_arr[0]
for i in range(len(f_arr)):
if f_arr[i] <= f:
f = f_arr[i] - 1
... | minimum-deletions-to-make-character-frequencies-unique | Python3 || hashMap | sagarhasan273 | 0 | 3 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,914 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2208332/Python-or-Easy-solution-using-hashmap | class Solution:
def minDeletions(self, s: str) -> int:
# creating a hashtable to count each char frequrncy
d = {}
count = 0
uniquelst = []
for c in s:
d[c] = 1 + d.get(c,0)
for _,v in d.items():
while v > 0 and v in uniquelst:
... | minimum-deletions-to-make-character-frequencies-unique | Python | Easy solution using hashmap | __Asrar | 0 | 18 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,915 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2208318/Python3-oror-Very-Simple-Solution-oror-Easy-to-understand-oror-Greedy-approach | class Solution:
def minDeletions(self, s: str) -> int:
C=Counter(s)
l=list(C.values())
l.sort(reverse=True)
s=sum(l)
cnt=pr=l[0] #cnt for Count and pr for previous value
for i in l[1:]:
if i==pr:
cnt+=i-1
pr=i-1
... | minimum-deletions-to-make-character-frequencies-unique | Python3 || Very Simple Solution || Easy to understand || Greedy approach | aditya1292 | 0 | 4 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,916 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2207988/Python3-Solution | class Solution:
def minDeletions(self, s: str) -> int:
counter = Counter(s)
freq_set = set() # we use a set to keep track of the unique frequency count
res = 0
# sort the string in decreasing order by the frequency
#
for count in sorted(counter.values(), reverse = Tru... | minimum-deletions-to-make-character-frequencies-unique | Python3 Solution | M-Phuykong | 0 | 11 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,917 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2207490/Python3-Greedy-oror-hashset | class Solution:
def minDeletions(self, s: str) -> int:
hashset=set()
freq=Counter(s) # count the frequency of each character
ans=0
for j in freq.values():
if j not in hashset: # if the frequency is not yet appeared in hashset it means it is uniq... | minimum-deletions-to-make-character-frequencies-unique | Python3 Greedy || hashset | akshat12199 | 0 | 9 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,918 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2207322/Easy-to-understand! | class Solution:
def minDeletions(self, s: str) -> int:
"""
Make a dict named frequency, record the number of every frequency.
Traverse every frequency, if the number of it >1,
move one of it to frequency<i and the quantity==0,record the distance when moving frequency.
Worst ... | minimum-deletions-to-make-character-frequencies-unique | Easy to understand! | XRFXRF | 0 | 15 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,919 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2207146/Python-using-counter.-Time%3A-O(N).-Space%3A-O(N) | class Solution:
def minDeletions(self, s: str) -> int:
count = sorted(Counter(s).values())
result = 0
for idx in range(len(count) - 2, -1, -1):
if count[idx] >= count[idx + 1]:
result += min(count[idx], count[idx] - count[idx+1] + 1)
count[idx] = m... | minimum-deletions-to-make-character-frequencies-unique | Python, using counter. Time: O(N). Space: O(N) | blue_sky5 | 0 | 40 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,920 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2201069/Solution-using-DictionaryHash-with-detailed-explanation | class Solution:
def minDeletions(self, s: str) -> int:
charcountdict={}
for letter in s:
if letter not in charcountdict:
charcountdict[letter]=1
else:
charcountdict[letter]+=1
countlist=list(charcountdict.values())
removecount=0... | minimum-deletions-to-make-character-frequencies-unique | Solution using Dictionary/Hash with detailed explanation | yogeshwarb | 0 | 15 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,921 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/2177818/Python-easy-to-read-and-understand-or-hashmap | class Solution:
def minDeletions(self, s: str) -> int:
d = {}
for i in s:
d[i] = d.get(i, 0) + 1
seen = set()
items = sorted(d.values(), reverse=True)
ans = 0
for val in items:
while val in seen:
val -= 1
... | minimum-deletions-to-make-character-frequencies-unique | Python easy to read and understand | hashmap | sanial2001 | 0 | 33 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,922 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/1921268/Python3-Solution | class Solution:
def minDeletions(self, s: str) -> int:
cnt = 0
d = {}
auxSet = set({})
for i in s:
if i in d:
d[i] += 1
else:
d[i] = 1
distinctValuesList = d.values()
for cn in distinctValuesList:
... | minimum-deletions-to-make-character-frequencies-unique | Python3 Solution | DietCoke777 | 0 | 62 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,923 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/1798035/python-solution-using-max-heap | class Solution:
def minDeletions(self, s: str) -> int:
pq = [-v for k, v in Counter(s).items()]
heapify(pq)
count = 0
while pq:
v = heappop(pq)
if not pq:
break
vv = pq[0]
if v == vv:
if -v - 1 > 0:
... | minimum-deletions-to-make-character-frequencies-unique | python solution using max heap | 23333_tragedy | 0 | 74 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,924 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/1166996/Python-3-or-distributing-over-a-dictionary | class Solution:
def minDeletions(self, s: str) -> int:
#collect the frequency of the symbols in dictionary{symbol:frequency}
frequency={}
for i in s:
try:
frequency[i]=frequency[i]+1
except:
frequency[i]=1
working_list=l... | minimum-deletions-to-make-character-frequencies-unique | Python 3 | distributing over a dictionary | vjpn97 | 0 | 101 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,925 |
https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/discuss/1146503/Python3-easy-solution-using-dictionary | class Solution:
def minDeletions(self, s: str) -> int:
cnt_dict = {}
for c in s:
if c in cnt_dict:
cnt_dict[c]+=1
else:
cnt_dict[c]=1 #Store the frequency of each character in a dictionary
... | minimum-deletions-to-make-character-frequencies-unique | Python3 easy solution using dictionary | bPapan | 0 | 216 | minimum deletions to make character frequencies unique | 1,647 | 0.592 | Medium | 23,926 |
https://leetcode.com/problems/sell-diminishing-valued-colored-balls/discuss/927674/Python3-Greedy | class Solution:
def maxProfit(self, inventory: List[int], orders: int) -> int:
inventory.sort(reverse=True) # inventory high to low
inventory += [0]
ans = 0
k = 1
for i in range(len(inventory)-1):
if inventory[i] > inventory[i+1]:
if k*(inventor... | sell-diminishing-valued-colored-balls | [Python3] Greedy | ye15 | 32 | 5,200 | sell diminishing valued colored balls | 1,648 | 0.305 | Medium | 23,927 |
https://leetcode.com/problems/sell-diminishing-valued-colored-balls/discuss/927674/Python3-Greedy | class Solution:
def maxProfit(self, inventory: List[int], orders: int) -> int:
fn = lambda x: sum(max(0, xx - x) for xx in inventory) # balls sold
# last true binary search
lo, hi = 0, 10**9
while lo < hi:
mid = lo + hi + 1 >> 1
if fn(mid) >= orders: l... | sell-diminishing-valued-colored-balls | [Python3] Greedy | ye15 | 32 | 5,200 | sell diminishing valued colored balls | 1,648 | 0.305 | Medium | 23,928 |
https://leetcode.com/problems/sell-diminishing-valued-colored-balls/discuss/2807111/Python3-Solution-or-Sorting-or-O(nlogn) | class Solution:
def maxProfit(self, A, O):
nsum = lambda n : (n * (n + 1)) // 2
A.sort(reverse = True)
A.append(0)
ans, mod = 0, 10 ** 9 + 7
for i in range(len(A) - 1):
if (i + 1) * (A[i] - A[i + 1]) > O:
k, l = O // (i + 1), O % (i + 1)
... | sell-diminishing-valued-colored-balls | β Python3 Solution | Sorting | O(nlogn) | satyam2001 | 1 | 43 | sell diminishing valued colored balls | 1,648 | 0.305 | Medium | 23,929 |
https://leetcode.com/problems/sell-diminishing-valued-colored-balls/discuss/1306530/Python-Binary-Search-and-Arithmetic-series-formula-680ms | class Solution:
def maxProfit(self, inventory: List[int], orders: int) -> int:
inventory.sort(reverse=True)
inventory.append(0)
p = 0
for i in range(10**5):
if inventory[i]>inventory[i+1]:
if (i+1)*(inventory[i]-inventory[i+1])>=orders:
... | sell-diminishing-valued-colored-balls | Python Binary Search & Arithmetic series formula 680ms | johnnylu305 | 1 | 743 | sell diminishing valued colored balls | 1,648 | 0.305 | Medium | 23,930 |
https://leetcode.com/problems/sell-diminishing-valued-colored-balls/discuss/1970689/Python3-or-Binary-Search | class Solution:
def maxProfit(self, inventory: List[int], orders: int) -> int:
left = 0
right = max(inventory)
while right - left > 1:
mid = left + (right - left) // 2
sold_balls = sum(inv - mid for inv in inventory if inv > mid)
... | sell-diminishing-valued-colored-balls | Python3 | Binary Search | showing_up_each_day | -1 | 207 | sell diminishing valued colored balls | 1,648 | 0.305 | Medium | 23,931 |
https://leetcode.com/problems/sell-diminishing-valued-colored-balls/discuss/1934512/Python3-Greedy-READABLE-Solution-for-Dummies-by-a-Dummy | class Solution:
def maxProfit(self, inventory: List[int], orders: int) -> int:
inventory.sort(reverse=True)
N = len(inventory)
width = 1
total = 0
# sum of Arthematic Progression
def sumAP(startHeight, endHeight):
sum1 = startHeight * (startHeight... | sell-diminishing-valued-colored-balls | Python3 Greedy READABLE Solution for Dummies by a Dummy | moreCreativeUsername | -1 | 141 | sell diminishing valued colored balls | 1,648 | 0.305 | Medium | 23,932 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1903674/Python-Solution | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
if k == 0:
return [0] * len(code)
data = code + code
result = [sum(data[i + 1: i + 1 + abs(k)]) for i in range(len(code))]
# result = []
# for i in range(len(code)):
# result.append(sum... | defuse-the-bomb | Python Solution | hgalytoby | 2 | 142 | defuse the bomb | 1,652 | 0.612 | Easy | 23,933 |
https://leetcode.com/problems/defuse-the-bomb/discuss/2677931/Python3-oror-TC-O(N)SCO(1)-oror-Optimized-Solution | class Solution:
def helper(self,code,k):
res = []
total = sum(code[1:k+1])
res.append(total)
i,j = 1,k+1
while i<len(code):
if j==len(code):
j = 0
total+=-code[i]+code[j]
res.append(total)
j+=1
i+=1
... | defuse-the-bomb | Python3 || TC = O(N),SC=O(1) || Optimized Solution | shacid | 0 | 15 | defuse the bomb | 1,652 | 0.612 | Easy | 23,934 |
https://leetcode.com/problems/defuse-the-bomb/discuss/2640698/An-Approach-Using-Modulo-and-Nested-Loops | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
if k == 0:
return [0] * len(code)
cracked = [] * len(code)
if k < 0:
for i in range(len(code)):
total = 0
for j in range(i - 1, i + k - 1, -1):
... | defuse-the-bomb | An Approach Using Modulo and Nested Loops | kcstar | 0 | 11 | defuse the bomb | 1,652 | 0.612 | Easy | 23,935 |
https://leetcode.com/problems/defuse-the-bomb/discuss/2522873/Python-3-or-easy-to-understand | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
# replace every number with 0
if k == 0:
return [0] * len(code)
sum = 0
res = []
if k > 0:
left = 0
# for 0th number, get sum of next k numbers
# Note: k <... | defuse-the-bomb | Python 3 | easy to understand | user7726Y | 0 | 25 | defuse the bomb | 1,652 | 0.612 | Easy | 23,936 |
https://leetcode.com/problems/defuse-the-bomb/discuss/2440184/Python3 | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
if k>0:
code+=code[:k]
for i in range(len(code)-k):
summ=0
for j in range(i+1,i+k+1):
summ+=code[j]
code... | defuse-the-bomb | Python3 | aditya_maskar | 0 | 43 | defuse the bomb | 1,652 | 0.612 | Easy | 23,937 |
https://leetcode.com/problems/defuse-the-bomb/discuss/2440184/Python3 | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
if k>0:
code+=code[:k]
for i in range(len(code)-k):
code[i]= sum(code[i+1:i+k+1])
code = code[:len(code)-k]
elif k<0:
... | defuse-the-bomb | Python3 | aditya_maskar | 0 | 43 | defuse the bomb | 1,652 | 0.612 | Easy | 23,938 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1895586/Python3-Simple-Easy-to-Understand-List-Generator | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
n=len(code)
return [
sum(
[
code[(i+1+l)%n]
if k>=0
else
code[(i-1-l)%n]
... | defuse-the-bomb | Python3 Simple Easy to Understand List Generator | Forest-Dewberry | 0 | 56 | defuse the bomb | 1,652 | 0.612 | Easy | 23,939 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1816147/5-Lines-Python-Solution-oror-90-Faster(40ms)-oror-Memory-less-than-92 | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
ans=[] ; l=len(code) ; code_=code*3
if k==0: return [0]*l
elif k>0: [ans.append(sum(code_[l+i+1:l+i+k+1])) for i in range(l)]
else: [ans.append(sum(code_[l+i+k:l+i])) for i in range(l)]
return ans | defuse-the-bomb | 5-Lines Python Solution || 90% Faster(40ms) || Memory less than 92% | Taha-C | 0 | 43 | defuse the bomb | 1,652 | 0.612 | Easy | 23,940 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1763942/Python-dollarolution | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
v = code*2
if k < 0:
for i in range(len(v)//2,len(v)):
code[i-len(code)] = sum(v[i+k:i])
else:
for i in range(len(code)):
code[i] = sum(v[i+1:i+k+1])
retur... | defuse-the-bomb | Python $olution | AakRay | 0 | 46 | defuse the bomb | 1,652 | 0.612 | Easy | 23,941 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1750935/Python-Easy-Solution | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
l = len(code)
ans = code
code = code + code + code
for i in range(l,2*l):
ans[i-l] =0
if k == 0:
return [0]*l
elif k > 0:
for j in range(1,k+1)... | defuse-the-bomb | Python Easy Solution | MengyingLin | 0 | 52 | defuse the bomb | 1,652 | 0.612 | Easy | 23,942 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1241434/Python-O(n)-time-O(1)-additional-space-Re-use-array | class Solution:
def decrypt(self, A: List[int], k: int) -> List[int]:
n = len(A)
mod = 101
if k == 0:
for i in range(n):
A[i] = 0
return A
start, end = 1, k
if k < 0:
k = -k
start, end = n - k, n - 1
cu... | defuse-the-bomb | Python O(n) time, O(1) additional space, Re-use array | omars1515 | 0 | 164 | defuse the bomb | 1,652 | 0.612 | Easy | 23,943 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1026846/Python3-via-prefix-sum | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
n = len(code)
code *= 2 # augmenting array
prefix = [0] # prefix sum (w/ leading 0)
for x in code: prefix.append(prefix[-1] + x)
ans = []
for i in range(n):
if k >... | defuse-the-bomb | [Python3] via prefix sum | ye15 | 0 | 81 | defuse the bomb | 1,652 | 0.612 | Easy | 23,944 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1026846/Python3-via-prefix-sum | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
if k < 0: return self.decrypt(code[::-1], -k)[::-1]
prefix = [0] # prefix sum (w/ leading 0)
for x in code*2: prefix.append(prefix[-1] + x)
ans = []
for i in range(len(code)):
... | defuse-the-bomb | [Python3] via prefix sum | ye15 | 0 | 81 | defuse the bomb | 1,652 | 0.612 | Easy | 23,945 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1026846/Python3-via-prefix-sum | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
if k < 0: return self.decrypt(code[::-1], -k)[::-1]
ans, x = [], 0 # range sum
for i in range(len(code) + k):
x += code[i%len(code)]
if i >= k:
x -= code[i-k]
a... | defuse-the-bomb | [Python3] via prefix sum | ye15 | 0 | 81 | defuse the bomb | 1,652 | 0.612 | Easy | 23,946 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1013248/Python3-easy-solution. | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
size=len(code)
temp=nums*2
if k>0:
for i in range(size):
code[i]=sum(temp[i+1:i+k+1])
elif k<0:
for i in range(size,size*2):
code[i-size]=sum(temp[i-1:i+k-1:-1])... | defuse-the-bomb | Python3 easy solution. | heimdall2308 | 0 | 71 | defuse the bomb | 1,652 | 0.612 | Easy | 23,947 |
https://leetcode.com/problems/defuse-the-bomb/discuss/943365/Intuitive-approach | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
decoded_nums = []
def decode(pos, k=k, code=code):
decode_num = 0
if k != 0:
for i in range(1, abs(k)+1):
new_pos = pos + (i if k > 0 else -i)
... | defuse-the-bomb | Intuitive approach | puremonkey2001 | 0 | 55 | defuse the bomb | 1,652 | 0.612 | Easy | 23,948 |
https://leetcode.com/problems/defuse-the-bomb/discuss/936785/Python-Explanation-Doubling-length-of-array | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
"""
If k > 0, replace the ith number with the sum of the next k numbers.
If k < 0, replace the ith number with the sum of the previous k numbers.
If k == 0, replace the ith number with 0.
"""
... | defuse-the-bomb | Python β Explanation β Doubling length of array | ericklarac | 0 | 96 | defuse the bomb | 1,652 | 0.612 | Easy | 23,949 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1073821/Python3-simple-and-easy-to-understand-solution | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
n = len(code)
z = code.copy()
y = code.copy()
if k == 0:
z = [0]*n
elif k > 0:
y += code
for i in range(n):
z[i] = sum(y[i+1 : i+k+1])
else:
... | defuse-the-bomb | Python3 simple and easy to understand solution | EklavyaJoshi | -1 | 118 | defuse the bomb | 1,652 | 0.612 | Easy | 23,950 |
https://leetcode.com/problems/defuse-the-bomb/discuss/1016113/Ultra-Simple-CppPython3-Solution-or-Suggestions-for-optimization-are-welcomed-or | class Solution:
def decrypt(self, code: List[int], k: int) -> List[int]:
s=0
temp=0
ans=[]
for i in range(0,len(code)):
temp=k
s=0
if temp==0:
ans.append(0)
elif temp>... | defuse-the-bomb | Ultra Simple Cpp/Python3 Solution | Suggestions for optimization are welcomed | | angiras_rohit | -1 | 90 | defuse the bomb | 1,652 | 0.612 | Easy | 23,951 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1020107/Python-DP-solution-easy-to-understand | class Solution:
def minimumDeletions(self, s: str) -> int:
# track the minimum number of deletions to make the current string balanced ending with 'a', 'b'
end_a, end_b = 0,0
for val in s:
if val == 'a':
# to end with 'a', nothing to do with previous ending with ... | minimum-deletions-to-make-string-balanced | [Python] DP solution easy to understand | cloverpku | 12 | 1,100 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,952 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1912995/Python-easy-to-read-and-understand-or-stack | class Solution:
def minimumDeletions(self, s: str) -> int:
stack, res = [], 0
for i in range(len(s)):
if stack and s[i] == "a" and stack[-1] == "b":
stack.pop()
res += 1
else:
stack.append(s[i])
return res | minimum-deletions-to-make-string-balanced | Python easy to read and understand | stack | sanial2001 | 6 | 231 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,953 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1452670/Python-Stack-or-Beats-95 | class Solution:
def minimumDeletions(self, s: str) -> int:
count = 0
stack = []
for c in s:
if c == 'b':
stack.append(c)
elif stack:
stack.pop()
count += 1
return count | minimum-deletions-to-make-string-balanced | Python Stack | Beats 95% | russellh | 4 | 294 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,954 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1093898/Python3-greedy | class Solution:
def minimumDeletions(self, s: str) -> int:
ans = suffix = 0
for c in reversed(s):
if c == "a": suffix += 1
else: ans = min(1 + ans, suffix)
return ans | minimum-deletions-to-make-string-balanced | [Python3] greedy | ye15 | 3 | 128 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,955 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1948314/WEEB-DOES-PYTHONC%2B%2B-PREFIX-%2B-SUFFIX-SUM | class Solution:
def minimumDeletions(self, s: str) -> int:
preSum = [0] * (len(s) + 1)
sufSum = [0] * (len(s) + 1)
for i in range(len(s)):
if s[i] == "a":
preSum[i] += 1 + preSum[i-1]
else:
preSum[i] = preSum[i-1]
if s[len(s)-i-1] == "b":
sufSum[len(s)-i-1] += 1 + sufSum[len(s)-i]
els... | minimum-deletions-to-make-string-balanced | WEEB DOES PYTHON/C++ PREFIX + SUFFIX SUM | Skywalker5423 | 1 | 112 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,956 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/2793615/Python-98-beats-Time | class Solution:
def minimumDeletions(self, s: str) -> int:
l=len(s)
i=0
while i<l and s[i]!='b':
i+=1
j=l-1
while j>=0 and s[j]!='a':
j-=1
if i==l or j==-1:
return 0
ca=0
cb=0
ans=0
for k in... | minimum-deletions-to-make-string-balanced | Python 98% beats Time | RjRahul003 | 0 | 3 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,957 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/2687358/Python3-or-Linear-DP | class Solution:
def minimumDeletions(self, s: str) -> int:
cntB=0
n=len(s)
dp=[0]*n
for i in range(n):
if s[i]=='b':
cntB+=1
dp[i]=dp[i-1] if i>=1 else dp[i]
else:
val=dp[i-1]+1 if i>=1 else 1
dp[... | minimum-deletions-to-make-string-balanced | [Python3] | Linear DP | swapnilsingh421 | 0 | 12 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,958 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/2016830/Python-solution-with-O(n)-using-stack | class Solution:
def minimumDeletions(self, s: str) -> int:
stack = []
res = 0
for i in s:
# checking stack is empty or not to avoid errors
#if last element in stack is 'b' then the present character is 'a' this case will wrong
# so we pop the stack and increse the count
i... | minimum-deletions-to-make-string-balanced | Python solution with O(n) using stack | Vamsidhar01 | 0 | 129 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,959 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1952754/Python3-Solution-with-using-dp-approach | class Solution:
"""
if char == 'b' -> no more cost
if char == 'a':
1. move all 'b' before 'a' -> cost += cnt_b
2. remove 'a' -> cost += 1
"""
def minimumDeletions(self, s: str) -> int:
res = 0
cnt_b = 0
for char in s:
if char == 'b':
... | minimum-deletions-to-make-string-balanced | [Python3] Solution with using dp approach | maosipov11 | 0 | 83 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,960 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1348883/Easy-to-understand%3A-Python-solution | class Solution:
def minimumDeletions(self, s: str) -> int:
# consider each index as b start
# count left_a, left_b, right_a, right_b on L/R at each point
# num_changes = left_b + right_a
counts = {}
n = len(s)
left_a, left_b, right_a, right_b = 0, 0, s.count('a'), s.c... | minimum-deletions-to-make-string-balanced | Easy to understand: Python solution | sherryfansf | 0 | 257 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,961 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1245883/easy-to-understand-greedy-python | class Solution:
def minimumDeletions(self, s: str) -> int:
n = float("inf")
na = s.count("a")
nb = 0
for i in range(len(s)):
if s[i]=="a":
na -= 1
elif s[i]=="b":
nb += 1
n = min(na + nb,n)
return min(n,s.co... | minimum-deletions-to-make-string-balanced | easy to understand, greedy, python | albertnew2018 | 0 | 153 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,962 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1182576/Python%3A-Two-Ideas | class Solution:
def minimumDeletions(self, s: str) -> int:
def recurse(i, j):
if i == len(s): return 0
return recurse(i+1, j) + (s[i] == ('b' if i <= j else 'a'))
return min([recurse(0, j) for j in range(-1, len(s)+1)]) | minimum-deletions-to-make-string-balanced | Python: Two Ideas | dev-josh | 0 | 189 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,963 |
https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/discuss/1182576/Python%3A-Two-Ideas | class Solution:
def minimumDeletions(self, s: str) -> int:
a_idx = [0] + list(itertools.accumulate([1 if c == 'a' else 0 for c in s]))
b_idx = [0] + list(itertools.accumulate([1 if c == 'b' else 0 for c in s]))
result = float('inf')
for idx ... | minimum-deletions-to-make-string-balanced | Python: Two Ideas | dev-josh | 0 | 189 | minimum deletions to make string balanced | 1,653 | 0.588 | Medium | 23,964 |
https://leetcode.com/problems/minimum-jumps-to-reach-home/discuss/1540090/Simple-BFS-oror-Clean-and-Concise-oror-Well-coded | class Solution:
def minimumJumps(self, forbidden: List[int], a: int, b: int, x: int) -> int:
forbidden = set(forbidden)
limit = max(x,max(forbidden))+a+b
seen = set()
q = [(0,0,False)]
while q:
p,s,isb = q.pop(0)
if p>limit or p<0 or p in forbidden or (p,isb) in seen:
... | minimum-jumps-to-reach-home | ππ Simple BFS || Clean & Concise || Well-coded π | abhi9Rai | 4 | 618 | minimum jumps to reach home | 1,654 | 0.287 | Medium | 23,965 |
https://leetcode.com/problems/minimum-jumps-to-reach-home/discuss/1052778/Python-Easy-understanding-BFS-solution-with-explanation | class Solution:
def minimumJumps(self, forbidden: List[int], a: int, b: int, x: int) -> int:
forbidden = set(forbidden)
visited = set()
limit = max(x, max(forbidden)) + a + b
queue = [(0, 0, False)]
while queue:
pos, step, back = queue.pop(0)
if pos > ... | minimum-jumps-to-reach-home | [Python] Easy-understanding BFS solution with explanation | Pandede | 2 | 413 | minimum jumps to reach home | 1,654 | 0.287 | Medium | 23,966 |
https://leetcode.com/problems/minimum-jumps-to-reach-home/discuss/2522402/Python3-BFS-and-My-Interpretation-of-How-to-Find-The-Upper-Bound | class Solution1:
def minimumJumps(self, forbidden: List[int], a: int, b: int, x: int) -> int:
"""Very difficult one. I had the intuition correct, that by using BFS,
we can always find the solution when x is reachable. The difficulty is
when x is not reachable. Since we can always add a, ther... | minimum-jumps-to-reach-home | [Python3] BFS and My Interpretation of How to Find The Upper Bound | FanchenBao | 1 | 85 | minimum jumps to reach home | 1,654 | 0.287 | Medium | 23,967 |
https://leetcode.com/problems/minimum-jumps-to-reach-home/discuss/2130948/PYTHON-BFS-Solution-beats-93.98 | class Solution:
def minimumJumps(self, fb: List[int], a: int, b: int, x: int) -> int:
fb = set(fb)
q = deque([[0,0,True]])
while(q):
n,l,isf = q.popleft()
if(n<0 or n in fb or n>2000+2*b):
continue
fb.add(n)
if(n==x):
... | minimum-jumps-to-reach-home | [PYTHON] BFS Solution beats 93.98% | markintosh_andrew | 1 | 169 | minimum jumps to reach home | 1,654 | 0.287 | Medium | 23,968 |
https://leetcode.com/problems/minimum-jumps-to-reach-home/discuss/1103362/Python3-bfs | class Solution:
def minimumJumps(self, forbidden: List[int], a: int, b: int, x: int) -> int:
forbidden = set(forbidden)
upper = max(forbidden | {x}) + a + b
ans = 0
queue = [(0, 0)]
forbidden.add(0)
while queue:
newq = []
for n, k in ... | minimum-jumps-to-reach-home | [Python3] bfs | ye15 | 1 | 229 | minimum jumps to reach home | 1,654 | 0.287 | Medium | 23,969 |
https://leetcode.com/problems/minimum-jumps-to-reach-home/discuss/2811470/Python-(Simple-BFS) | class Solution:
def minimumJumps(self, forbidden, a, b, x):
limit, visited = max(x,max(forbidden)) + a + b, set()
stack = [(0,0,False)]
while stack:
val, steps, par = stack.pop(0)
if val > limit or val < 0 or val in forbidden or (val,par) in visited:
... | minimum-jumps-to-reach-home | Python (Simple BFS) | rnotappl | 0 | 5 | minimum jumps to reach home | 1,654 | 0.287 | Medium | 23,970 |
https://leetcode.com/problems/distribute-repeating-integers/discuss/1103429/Python3-backtracking | class Solution:
def canDistribute(self, nums: List[int], quantity: List[int]) -> bool:
freq = {}
for x in nums: freq[x] = 1 + freq.get(x, 0)
vals = sorted(freq.values(), reverse=True)
quantity.sort(reverse=True) # pruning - large values first
def fn(i):
... | distribute-repeating-integers | [Python3] backtracking | ye15 | 2 | 159 | distribute repeating integers | 1,655 | 0.392 | Hard | 23,971 |
https://leetcode.com/problems/distribute-repeating-integers/discuss/1489459/simple-dfs-with-bitmasking | class Solution:
def canDistribute(self, nums: List[int], quantity: List[int]) -> bool:
arr=[0]*(1001)
for i in nums:
arr[i]+=1
arr.sort(reverse=True)
#print(arr)
arr=arr[:10]
dict={}
def dfs(ind,arr):
if ind==m:
... | distribute-repeating-integers | simple dfs with bitmasking | heisenbarg | 1 | 106 | distribute repeating integers | 1,655 | 0.392 | Hard | 23,972 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/935962/Python3-2-line-via-counter | class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
cnt1, cnt2 = Counter(word1), Counter(word2)
return cnt1.keys() == cnt2.keys() and sorted(cnt1.values()) == sorted(cnt2.values()) | determine-if-two-strings-are-close | [Python3] 2-line via counter | ye15 | 2 | 112 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,973 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/935962/Python3-2-line-via-counter | class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
def fn(word):
"""Return freq table of word."""
freq = {}
for c in word: freq[c] = 1 + freq.get(c, 0)
return freq
freq1, freq2 = fn(word1), fn(word2)
retur... | determine-if-two-strings-are-close | [Python3] 2-line via counter | ye15 | 2 | 112 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,974 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/1029096/Python%3A-2-lines-solution-with-counter | class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
c1, c2 = Counter(word1), Counter(word2)
return c1.keys() == c2.keys() and Counter(c1.values()) == Counter(c2.values()) | determine-if-two-strings-are-close | Python: 2 lines solution with counter | jaykim9438 | 1 | 71 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,975 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/935972/PYTHON-oror-NLOGN-oror-FAST-oror-EASY | class Solution(object):
def closeStrings(self, word1, word2):
count1 = dict()
count2 = dict()
for char in word1:
count1[char] = count1.get(char, 0) + 1
for char in word2:
count2[char] = count2.get(char,0) + 1
return ... | determine-if-two-strings-are-close | PYTHON || NLOGN || FAST || EASY | akashgkrishnan | 1 | 113 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,976 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/2520906/Python3-solution-or-Explained | class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
"""
operation 1 ~ swapping characters - this means that the order of the characters is not a problem since we can move them as we want
operation 2 ~ same number of keys and same values at values - using 2 hashmaps of the... | determine-if-two-strings-are-close | Python3 solution | Explained | FlorinnC1 | 0 | 11 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,977 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/2111798/python-3-oror-simple-hash-map-solution-oror-O(n)O(1) | class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
count1 = collections.Counter(word1)
count2 = collections.Counter(word2)
return (count1.keys() == count2.keys() and
sorted(count1.values()) == sorted(count2.values())) | determine-if-two-strings-are-close | python 3 || simple hash map solution || O(n)/O(1) | dereky4 | 0 | 38 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,978 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/1661886/Python-simple-O(n)-time-O(1)-space-solution-(if-else-statements-only) | class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
cnt1, cnt2 = defaultdict(int), defaultdict(int)
n1, n2 = defaultdict(int), defaultdict(int)
s1, s2 = set(), set()
for w in word1:
cnt1[w] += 1
s1.add(w)
... | determine-if-two-strings-are-close | Python simple O(n) time, O(1) space solution (if-else statements only) | byuns9334 | 0 | 87 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,979 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/1029506/Python-84-solution-collections.Counter-method | class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
word1_cnt = collections.Counter(word1)
word2_cnt = collections.Counter(word2)
# precondition 1
if set([k for k in word1_cnt]) != set([k for k in word2_cnt]):
return False
# precondition 2
cnt1 = c... | determine-if-two-strings-are-close | Python 84% solution, collections.Counter method | yilingliu | 0 | 47 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,980 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/1029168/python3%3A-sorting-O(no.-of-chars-*-no.-of-chars)time-and-O(no-of-chars)-space | class Solution:
def closeStrings(self, word1: str, word2: str) -> bool:
dp1=[0]*26
dp2=[0]*26
n1=len(word1)
n2=len(word2)
if(n1!=n2):
return False
for i in range(n1):
dp1[ord(word1[i])-97]+=1
dp2[ord(word2[i])-97]+=1
... | determine-if-two-strings-are-close | python3: sorting O(no. of chars * no. of chars)time and O(no of chars) space | _Rehan12 | 0 | 23 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,981 |
https://leetcode.com/problems/determine-if-two-strings-are-close/discuss/947260/Python-7-Line-easy-solution-with-Dictionary. | class Solution(object):
def closeStrings(self, word1, word2):
def ans(word):
dict1 ={}
for i in word:
if i not in dict1:dict1[i]=1
else:dict1[i]+=1
return sorted(dict1.values())
return ans(word1)==ans(word2) and set(word1)==set(word... | determine-if-two-strings-are-close | [Python] 7 Line easy solution with Dictionary. | rachitsxn292 | 0 | 193 | determine if two strings are close | 1,657 | 0.541 | Medium | 23,982 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/935986/Python3-O(N)-hash-table-of-prefix | class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
mp = {0: 0}
prefix = 0
for i, num in enumerate(nums, 1):
prefix += num
mp[prefix] = i
ans = mp.get(x, inf)
for i, num in enumerate(reversed(nums), 1):
... | minimum-operations-to-reduce-x-to-zero | [Python3] O(N) hash table of prefix | ye15 | 21 | 1,600 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,983 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/935986/Python3-O(N)-hash-table-of-prefix | class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
x = sum(nums) - x
if not x: return len(nums) # edge case
seen = {0: -1}
ans = prefix = 0
for i, num in enumerate(nums):
prefix += num
if prefix - x in seen: ans = max(a... | minimum-operations-to-reduce-x-to-zero | [Python3] O(N) hash table of prefix | ye15 | 21 | 1,600 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,984 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/2136561/Python-or-Prefix-Sum-and-Sliding-Window-or-With-Explanation | class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
# find the longest subarray that sum to 'goal'
s = sum(nums)
n = len(nums)
goal = s - x
max_length = -1
left = 0
current_sum = 0
for right, num in enumerate(nums):
cu... | minimum-operations-to-reduce-x-to-zero | Python | Prefix Sum & Sliding Window | With Explanation | Mikey98 | 7 | 969 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,985 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/2136863/python-3-oror-simple-sliding-window-solution-oror-O(n)O(1) | class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
targetSum = sum(nums) - x
if targetSum <= 0:
return -1 if targetSum < 0 else len(nums)
largestWindow = -1
curSum = 0
left = 0
for right, num in enumerate(nums):
... | minimum-operations-to-reduce-x-to-zero | python 3 || simple sliding window solution || O(n)/O(1) | dereky4 | 2 | 175 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,986 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/2139813/Simple-Python-Solution-using-sliding-window-!! | class Solution(object):
def minOperations(self, li, x):
numsSum = sum(li)
k = numsSum-x
n = len(li)
i, j, s = 0, 0, 0
l = []
maximum = float('-inf')
if numsSum == x:
return n
if k>0:
while j < n:
s += li[j]
... | minimum-operations-to-reduce-x-to-zero | Simple Python Solution using sliding window !! | Namangarg98 | 1 | 96 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,987 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/2136778/Python-90-FASTER-96-memory-EFFICIENT-OPTIMAL-solution-O(n)-time-O(1)-space | class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
target = sum(nums) - x
curr_sum, max_len = 0, 0
start_idx = 0
found = False
for end_idx in range(len(nums)):
curr_sum += nums[end_idx]
while start_idx <= end_idx and curr_sum > target:
curr_sum -= nu... | minimum-operations-to-reduce-x-to-zero | Python 90% FASTER 96% memory EFFICIENT OPTIMAL solution O(n) time O(1) space | anuvabtest | 1 | 102 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,988 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/936064/Python3 | class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
ans = float("inf")
dicti = {0:0}
count = 0
for i in range(len(nums)):
count += nums[i]
if(count==x):
ans = min(ans,i+1)
dicti[count] = i+1
... | minimum-operations-to-reduce-x-to-zero | Python3 | swap2001 | 1 | 152 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,989 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/2275989/Python-Prefix-Sum | class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
if min(nums)>x or sum(nums)<x:
return -1
n = len(nums)
target = sum(nums)-x
# we need to find the longest substring sum = target
longest = 0
hash_map = {0:-1}
sum_ = 0
... | minimum-operations-to-reduce-x-to-zero | Python Prefix Sum | Abhi_009 | 0 | 68 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,990 |
https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/2155127/Python3-Solution-with-using-two-pointers | class Solution:
def minOperations(self, nums: List[int], x: int) -> int:
total_sum = sum(nums)
left, right = 0, 0
max_len_win = -1
current_win_sum = 0
while right < len(nums):
current_win_sum += nums[right]
while current_... | minimum-operations-to-reduce-x-to-zero | [Python3] Solution with using two-pointers | maosipov11 | 0 | 39 | minimum operations to reduce x to zero | 1,658 | 0.376 | Medium | 23,991 |
https://leetcode.com/problems/maximize-grid-happiness/discuss/1132982/Python3-top-down-dp | class Solution:
def getMaxGridHappiness(self, m: int, n: int, introvertsCount: int, extrovertsCount: int) -> int:
@cache
def fn(prev, i, j, intro, extro):
"""Return max grid happiness at (i, j)."""
if i == m: return 0 # no more position
if j == n: return... | maximize-grid-happiness | [Python3] top-down dp | ye15 | 1 | 319 | maximize grid happiness | 1,659 | 0.384 | Hard | 23,992 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/944697/Python-3-or-Python-1-liner-or-No-explanation | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | Python 3 | Python 1-liner | No explanation π | idontknoooo | 15 | 1,200 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 23,993 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2740932/Python-Simple-and-Easy-Way-to-Solve-or-99-Faster | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return True if ''.join(word1) == ''.join(word2) else False | check-if-two-string-arrays-are-equivalent | βοΈ Python Simple and Easy Way to Solve | 99% Faster π₯ | pniraj657 | 6 | 898 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 23,994 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/1163967/Python3-Simple-and-Single-Line-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
return ''.join(word1) == ''.join(word2) | check-if-two-string-arrays-are-equivalent | [Python3] Simple and Single Line Solution | VoidCupboard | 6 | 218 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 23,995 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2060799/PYTHON-Simple-Solution | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
str1=str2=""
for i in word1:
str1+=i
for j in word2:
str2+=j
return(bool(str1==str2)) | check-if-two-string-arrays-are-equivalent | PYTHON {Simple} Solution | tusharkhanna575 | 4 | 208 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 23,996 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/1338601/Python-O(1)-space-complexity-with-optimized-time-complexity-as-well.-EXPLAINED | class Solution:
def arrayStringsAreEqual(self, word1, word2):
"""
The below solutions take O(n + m) time and space, as we are
* Iterating through both list (Time complexity : O(n+m))
* Adding strings each time. In the backend a new string is created
with inc... | check-if-two-string-arrays-are-equivalent | Python O(1) space complexity with optimized time complexity as well. EXPLAINED | er1shivam | 4 | 256 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 23,997 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2743669/Python-oror-Easy-to-Understand-oror-Faster-than-93 | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s1 = ""
s2 = ""
for i in word1:
s1+=i
for i in word2:
s2+=i
return s1 == s2 | check-if-two-string-arrays-are-equivalent | Python || Easy to Understand || Faster than 93% | aniketbhamani | 1 | 4 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 23,998 |
https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/discuss/2741440/Python3ororO(N) | class Solution:
def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
s1=""
for i in word1:
s1+=i
s2=""
for j in word2:
s2+=j
return s1==s2 | check-if-two-string-arrays-are-equivalent | Python3||O(N) | Sneh713 | 1 | 45 | check if two string arrays are equivalent | 1,662 | 0.833 | Easy | 23,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.