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/smallest-string-with-swaps/discuss/1986130/python-graph-and-DFS-and-ASCII-and-busketsort-(commented)(Time-Onlogn-space-On)
class Solution: def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str: table = [] # name of our graph for i in range(len(s)) : table.append([]) # create the graph for pair in pairs : # and fill it table[pair[0]].append(pair[1]) table[pair[1]].append(pair[0]) letters = list(s) # split our string on characters for work with them visited = [0] * len(s) # for visited indexes in string s for DFS for i in range(len(s)) : # DFS if visited[i] == 0 : # if didn`t visit this index before indexes = set() # for group of all connected indexes with this start index cur_ind = [i] # stack for DFS (put into start index) cur_let = [0] * 26 # table where we will retain all letters from # all connected indexes (and in the same time this retaining will work as basketsort) while len(cur_ind) != 0 : # body of DFS idx = cur_ind.pop(-1) if visited[idx] == 0 : # if current index wasn't visited visited[idx] = 1 # check it as visited indexes.add(idx) # add it for group of all connected indexes cur_let[ord(letters[idx]) - 97] += 1 # put letter in this indexes into table for basketsort for next_i in table[idx] : # and try to continue to next connected indexes if visited[next_i] == 0 : cur_ind.append(next_i) j = 0 # rewrite all letters in lexicographical order for for idx in sorted(indexes) : # all indexes from our group (sorted in increasing order) while cur_let[j] == 0 : j += 1 letters[idx] = chr(97+j) cur_let[j] -= 1 return "".join(letters) # combine all letters as answer
smallest-string-with-swaps
python - graph & DFS & ASCII & busketsort (commented)(Time Onlogn, space - On)
ZX007java
0
33
smallest string with swaps
1,202
0.576
Medium
18,200
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1986059/Python3-solution-or-commented-and-explained
class Solution: def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str: """Getting all the indices (island_numbers) and letters (island) that need to be change""" def dfs(graph, node, been, island, letters, island_numbers): for i in range(len(graph[node])): if graph[node][i] not in been: been.add(graph[node][i]) island.append(letters[graph[node][i]]) island_numbers.append(graph[node][i]) dfs(graph, graph[node][i], been, island, letters, island_numbers) graph = {} # the graph that will store the nodes letters = {i: element for i, element in enumerate(s)} # dictionary to help transforming from indices to letters for i in range(len(pairs)): if pairs[i][0] not in graph: graph[pairs[i][0]] = [pairs[i][1]] else: graph[pairs[i][0]].append(pairs[i][1]) if pairs[i][1] not in graph: graph[pairs[i][1]] = [pairs[i][0]] else: graph[pairs[i][1]].append(pairs[i][0]) been = set() ans = list(s) for i in range(len(pairs)): if pairs[i][0] not in been: # if this node hasn't been visited yet island = [] island_numbers = [] dfs(graph, pairs[i][0], been, island, letters, island_numbers) island = sorted(island) island_numbers = sorted(island_numbers) for i in range(len(island_numbers)): ans[island_numbers[i]] = island[i] return "".join(ans)
smallest-string-with-swaps
Python3 solution | commented and explained
FlorinnC1
0
75
smallest string with swaps
1,202
0.576
Medium
18,201
https://leetcode.com/problems/smallest-string-with-swaps/discuss/1985673/Python3-solution
class Solution: def smallestStringWithSwaps(self, s: str, pairs: List[List[int]]) -> str: class UF: def __init__(self, n): self.p = list(range(n)) def union(self, x, y): self.p[self.find(x)] = self.find(y) def find(self, x): if x != self.p[x]: self.p[x] = self.find(self.p[x]) return self.p[x] uf, res, m = UF(len(s)), [], defaultdict(list) for x,y in pairs: uf.union(x,y) for i in range(len(s)): m[uf.find(i)].append(s[i]) for comp_id in m.keys(): m[comp_id].sort(reverse=True) for i in range(len(s)): res.append(m[uf.find(i)].pop()) return ''.join(res)
smallest-string-with-swaps
Python3 solution
nomanaasif9
0
81
smallest string with swaps
1,202
0.576
Medium
18,202
https://leetcode.com/problems/sort-items-by-groups-respecting-dependencies/discuss/1149266/Python3-topological-sort
class Solution: def sortItems(self, n: int, m: int, group: List[int], beforeItems: List[List[int]]) -> List[int]: for i in range(n): if group[i] == -1: group[i] = i + m # re-group graph0 = {} # digraph of groups indeg0 = [0]*(m+n) # indegree of groups graph1 = {} # digrpah of items indeg1 = [0]*n # indegree of items for i, x in enumerate(beforeItems): for xx in x: if group[xx] != group[i]: graph0.setdefault(group[xx], []).append(group[i]) indeg0[group[i]] += 1 graph1.setdefault(xx, []).append(i) indeg1[i] += 1 def fn(graph, indeg): """Return topological sort of graph using Kahn's algo.""" ans = [] stack = [k for k in range(len(indeg)) if indeg[k] == 0] while stack: n = stack.pop() ans.append(n) for nn in graph.get(n, []): indeg[nn] -= 1 if indeg[nn] == 0: stack.append(nn) return ans tp0 = fn(graph0, indeg0) if len(tp0) != len(indeg0): return [] tp1 = fn(graph1, indeg1) if len(tp1) != len(indeg1): return [] mp0 = {x: i for i, x in enumerate(tp0)} mp1 = {x: i for i, x in enumerate(tp1)} return sorted(range(n), key=lambda x: (mp0[group[x]], mp1[x]))
sort-items-by-groups-respecting-dependencies
[Python3] topological sort
ye15
2
123
sort items by groups respecting dependencies
1,203
0.506
Hard
18,203
https://leetcode.com/problems/unique-number-of-occurrences/discuss/393086/Solution-in-Python-3-(one-line)-(beats-100.00-)
class Solution: def uniqueOccurrences(self, A: List[int]) -> bool: return (lambda x: len(x) == len(set(x)))(collections.Counter(A).values()) - Junaid Mansuri (LeetCode ID)@hotmail.com
unique-number-of-occurrences
Solution in Python 3 (one line) (beats 100.00 %)
junaidmansuri
13
2,600
unique number of occurrences
1,207
0.709
Easy
18,204
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2553450/Python-simple-solution
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: from collections import Counter c = Counter(arr) return len(c) == len({c[x] for x in c})
unique-number-of-occurrences
Python simple solution
StikS32
4
197
unique number of occurrences
1,207
0.709
Easy
18,205
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2833225/Python-and-Cpp-Solution-in-O(n)
class Solution: def uniqueOccurrences(self, arr) : dict = {} for i in arr: dict[i] = arr.count(i) return len(dict.values()) == len(set(dict.values()))
unique-number-of-occurrences
Python and Cpp Solution in O(n)
rapid_killer_9
1
132
unique number of occurrences
1,207
0.709
Easy
18,206
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2805693/Python-Fast-solution-using-a-dictionary-and-a-set
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: d = {} for i in arr: d[i] = d.get(i, 0) + 1 return len(set(d.values())) == len(d)
unique-number-of-occurrences
[Python] Fast solution using a dictionary and a set
Mark_computer
1
7
unique number of occurrences
1,207
0.709
Easy
18,207
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2725337/Python3oror-O(N2)ororRuntime-61-ms-Beats-64.37-Memory-14.1-MB-Beats-32.48
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: dit={} for i in range(len(arr)): if arr[i] in dit: dit[arr[i]]+=1 else: dit[arr[i]]=1 ans=[] for i, j in enumerate(dit): if dit[j] in ans: return False else: ans.append(dit[j]) return True
unique-number-of-occurrences
Python3|| O(N^2)||Runtime 61 ms Beats 64.37% Memory 14.1 MB Beats 32.48%
Sneh713
1
38
unique number of occurrences
1,207
0.709
Easy
18,208
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2666698/Easy-way-to-find-Unique-Number-Occurrences
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: list=[] s = set() # Occurance of each element in list and added each value of the dictionary in the list occurance = Counter(arr) for value in occurance.values(): list.append(value) # Check for each element of the list if the element is present in set then return false ,if not then add to set. for x in list: if x in s: return False s.add(x) return True
unique-number-of-occurrences
Easy way to find Unique Number Occurrences
24Neha
1
626
unique number of occurrences
1,207
0.709
Easy
18,209
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2564335/python-fast-solution-using-dictionary-and-set
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: dict_arr = collections.Counter(arr) set_occurrences = {val for key, val in dict_arr.items()} return len(dict_arr) == len(set_occurrences)
unique-number-of-occurrences
python fast solution using dictionary and set
samanehghafouri
1
61
unique number of occurrences
1,207
0.709
Easy
18,210
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1735885/Python-Easy-to-Understand
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: #list to check values valuecheck = [] #iterate through distinct values for item in set(arr): valuecheck.append(arr.count(item)) return(len(valuecheck) == len(set(valuecheck)))
unique-number-of-occurrences
Python Easy to Understand
ovidaure
1
227
unique number of occurrences
1,207
0.709
Easy
18,211
https://leetcode.com/problems/unique-number-of-occurrences/discuss/497054/Python3-using-Dictionary-with-comments-(faster-than-99.91-and-less-than-100)
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: nums = {} # Create and add to dictionary for index in arr: if index not in nums: nums[index] = 1 else: nums[index] += 1 """ Add all values (number of occurences) of dictionary into an array. If there are duplicates, if statement will return false. If no duplicates, the else statement will never be executed. Thus we eventually return true. """ check = [] for key, value in nums.items(): if value not in check: check.append(value) else: return False return True
unique-number-of-occurrences
Python3 using Dictionary with comments (faster than 99.91% and less than 100%)
clxu
1
280
unique number of occurrences
1,207
0.709
Easy
18,212
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2845306/python-dictionary-solution
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: dictry={} for x in arr: if x not in dictry: dictry[x]=1 else: dictry[x]=dictry[x]+1 print(dictry) if len(set(arr))==len(set(dictry.values())): return True else: return False
unique-number-of-occurrences
python dictionary solution
sahityasetu1996
0
1
unique number of occurrences
1,207
0.709
Easy
18,213
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2844780/python-solution-beats-87.77
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: freq={} l=[] for i in arr: if i in freq: freq[i]+=1 else: freq[i]=1 for a,b in freq.items(): l.append(b) return(len(set(l))==len(l))
unique-number-of-occurrences
python solution beats 87.77%
Prabhleen_17
0
1
unique number of occurrences
1,207
0.709
Easy
18,214
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2833690/Using-count-function-beats-97
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: a=list(set(arr)) res=[] for i in a: res.append(arr.count(i)) for i in res: if res.count(i)>1: return False return True
unique-number-of-occurrences
Using count function , beats 97%
kmpravin5
0
1
unique number of occurrences
1,207
0.709
Easy
18,215
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2827718/python-code-for-beginners
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: a=list(set(arr)) print(a) l=[] for x in range(0,len(a)): l.append(arr.count(a[x])) l.sort() for x in range(0,len(l)-1): if(l[x]==l[x+1]): return False return True
unique-number-of-occurrences
python code for beginners
VIKASHVAR_R
0
2
unique number of occurrences
1,207
0.709
Easy
18,216
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2725597/O(n)-time-and-O(n)-space
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: freqs = {} for num in arr: freqs[num] = 1 + freqs.get(num, 0) unique_freqs = set() for freq in freqs.values(): if freq in unique_freqs: return False else: unique_freqs.add(freq) return True
unique-number-of-occurrences
O(n) time and O(n) space
gopakumarmohandas
0
7
unique number of occurrences
1,207
0.709
Easy
18,217
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2673461/Python%2BCounter
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: c=Counter(arr).values() return len(c)==len(set(c))
unique-number-of-occurrences
Python+Counter
Leox2022
0
4
unique number of occurrences
1,207
0.709
Easy
18,218
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2655032/unique-no-of-occurences
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: d=dict() for i in range(len(arr)): if arr[i] in d: d[arr[i]]+=1 else: d[arr[i]]=1 s=set() for val in d.values(): if val not in s: s.add(val) else: return False return True
unique-number-of-occurrences
unique no of occurences
shivansh2001sri
0
151
unique number of occurrences
1,207
0.709
Easy
18,219
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2561463/Python3-One-liner-no-set
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: return all(map(lambda fre: fre == 1, Counter(Counter(arr).values()).values()))
unique-number-of-occurrences
[Python3] One-liner, no set
DG_stamper
0
36
unique number of occurrences
1,207
0.709
Easy
18,220
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2541939/Python-hashmap-values-comparison-with-set
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: hashmap = {} for x in arr: if x in hashmap: hashmap[x] += 1 else: hashmap[x] = 1 return len(hashmap.values()) == len(set(hashmap.values()))
unique-number-of-occurrences
Python hashmap values comparison with set
saadbash
0
53
unique number of occurrences
1,207
0.709
Easy
18,221
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2482929/Python-or-Two-clear-lines-with-Counter-and-a-one-liner-version-with-the-walrus-operator
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: counter_values = Counter(arr).values() return len(set(counter_values)) == len(counter_values)
unique-number-of-occurrences
Python | Two clear lines with Counter and a one-liner version with the walrus operator
Wartem
0
83
unique number of occurrences
1,207
0.709
Easy
18,222
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2450295/Python-or-Faster-than-90-or-2-lines
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: counter = Counter(arr) return len(counter.keys()) == len(set(counter.values()))
unique-number-of-occurrences
Python | Faster than 90% | 2 lines
pivovar3al
0
74
unique number of occurrences
1,207
0.709
Easy
18,223
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2420307/Python-One-liner-95-faster
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: return len(occ := Counter(arr).values()) == len(set(occ))
unique-number-of-occurrences
Python One liner - 95% faster
VanarajanNatarajan
0
64
unique number of occurrences
1,207
0.709
Easy
18,224
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2268502/Python-1-Liner
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: return sorted(Counter(Counter(arr).values()).values(), reverse=True)[0] == 1
unique-number-of-occurrences
Python 1-Liner
amaargiru
0
68
unique number of occurrences
1,207
0.709
Easy
18,225
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2208877/Python-1-Liner-easy-to-understand!!!
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: return len(set(Counter(arr).values()))==len(Counter(arr))
unique-number-of-occurrences
Python 1-Liner, easy to understand!!!
XRFXRF
0
105
unique number of occurrences
1,207
0.709
Easy
18,226
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2146959/faster-than-91.74-of-Python3
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: return (lambda x: len(x) == len(set(x)))(collections.Counter(arr).values())
unique-number-of-occurrences
faster than 91.74% of Python3
writemeom
0
69
unique number of occurrences
1,207
0.709
Easy
18,227
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2129385/PYTHON-or-Simple-and-easy-python-solution
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: hashMap = {} for i in arr: hashMap[i] = 1 + hashMap.get(i, 0) o = [] for i in hashMap: o.append(hashMap[i]) return len(set(o)) == len(o)
unique-number-of-occurrences
PYTHON | Simple & easy python solution
shreeruparel
0
81
unique number of occurrences
1,207
0.709
Easy
18,228
https://leetcode.com/problems/unique-number-of-occurrences/discuss/2083261/Using-2-Hashmap-Faster-than-96
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: res = [] hashmap = {} for i in arr: if i not in hashmap: hashmap[i] = 1 else: hashmap[i] += 1 for i in hashmap.values(): res.append(i) hashmap2 = {} for i in res: if i not in hashmap2: hashmap2[i] = 1 else: hashmap2[i] += 1 count = 0 a = len(hashmap2.values()) for i in hashmap2.values(): if i == 1: count += 1 if a == count: return True else: return False
unique-number-of-occurrences
Using 2 Hashmap Faster than 96%
itsmeparag14
0
149
unique number of occurrences
1,207
0.709
Easy
18,229
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1999658/Four-Line-Solution-or-Python-3-or-Hashmap
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: res = defaultdict(int) for i in arr: res[i] += 1 return (len(res) == len(set(res.values())))
unique-number-of-occurrences
Four Line Solution | Python 3 | Hashmap
CeciliaDeng
0
38
unique number of occurrences
1,207
0.709
Easy
18,230
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1941345/easy-python-code-faster-than-97
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: d = {} x = [] for i in arr: if i in d: d[i] += 1 else: d[i] = 1 for i in d: x.append(d[i]) for i in x: if x.count(i) != 1: return False return True
unique-number-of-occurrences
easy python code, faster than 97%
dakash682
0
58
unique number of occurrences
1,207
0.709
Easy
18,231
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1887332/Python-easy-solution-using-sets-and-count
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: counts = [arr.count(i) for i in set(arr)] for i in set(counts): if counts.count(i) != 1: return False return True
unique-number-of-occurrences
Python easy solution using sets and count
alishak1999
0
111
unique number of occurrences
1,207
0.709
Easy
18,232
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1852264/Python-Multiple-Solutions!-One-Liners!
class Solution(object): def uniqueOccurrences(self, arr): occurances = Counter(arr).values() numberOfOccurances = Counter(occurances).values() for i in numberOfOccurances: if i > 1: return False return True
unique-number-of-occurrences
Python - Multiple Solutions! One Liners!
domthedeveloper
0
54
unique number of occurrences
1,207
0.709
Easy
18,233
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1852264/Python-Multiple-Solutions!-One-Liners!
class Solution(object): def uniqueOccurrences(self, arr): occurances = Counter(arr).values() numberOfOccurances = Counter(occurances).values() commonNumberOfOccurances = filter(lambda x: x!=1, numberOfOccurances) return len(commonNumberOfOccurances) == 0
unique-number-of-occurrences
Python - Multiple Solutions! One Liners!
domthedeveloper
0
54
unique number of occurrences
1,207
0.709
Easy
18,234
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1852264/Python-Multiple-Solutions!-One-Liners!
class Solution(object): def uniqueOccurrences(self, arr): return len(filter(lambda x:x != 1, Counter(Counter(arr).values()).values())) == 0
unique-number-of-occurrences
Python - Multiple Solutions! One Liners!
domthedeveloper
0
54
unique number of occurrences
1,207
0.709
Easy
18,235
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1852264/Python-Multiple-Solutions!-One-Liners!
class Solution(object): def uniqueOccurrences(self, arr): occurances = Counter(arr).values() numberOfOccurances = Counter(occurances).values() return sum(numberOfOccurances) == len(numberOfOccurances)
unique-number-of-occurrences
Python - Multiple Solutions! One Liners!
domthedeveloper
0
54
unique number of occurrences
1,207
0.709
Easy
18,236
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1852264/Python-Multiple-Solutions!-One-Liners!
class Solution(object): def uniqueOccurrences(self, arr): return (lambda x : sum(x) == len(x))(Counter(Counter(arr).values()).values())
unique-number-of-occurrences
Python - Multiple Solutions! One Liners!
domthedeveloper
0
54
unique number of occurrences
1,207
0.709
Easy
18,237
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1852264/Python-Multiple-Solutions!-One-Liners!
class Solution(object): def uniqueOccurrences(self, arr): occurances = Counter(arr).values() return len(occurances) == len(set(occurances))
unique-number-of-occurrences
Python - Multiple Solutions! One Liners!
domthedeveloper
0
54
unique number of occurrences
1,207
0.709
Easy
18,238
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1852264/Python-Multiple-Solutions!-One-Liners!
class Solution(object): def uniqueOccurrences(self, arr): return (lambda x : len(x) == len(set(x)))(Counter(arr).values())
unique-number-of-occurrences
Python - Multiple Solutions! One Liners!
domthedeveloper
0
54
unique number of occurrences
1,207
0.709
Easy
18,239
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1809489/2-Lines-Python-Solution-oror-97-Faster-(32ms)-oror-Memory-less-than-85
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: c = Counter(arr) ; c = [c[key] for key in c] return len(set(c))==len(c)
unique-number-of-occurrences
2-Lines Python Solution || 97% Faster (32ms) || Memory less than 85%
Taha-C
0
79
unique number of occurrences
1,207
0.709
Easy
18,240
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1711855/Easy-Python-Solution
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: dict = {} for i in arr: dict[i] = dict.get(i, 0) + 1 if len(set(dict.values())) == len(dict): return True else: return False
unique-number-of-occurrences
Easy Python Solution
ayush_kushwaha
0
97
unique number of occurrences
1,207
0.709
Easy
18,241
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1617120/Python-3-very-easy-solution
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: arr = collections.Counter(arr) return len(set(arr.values())) == len(arr)
unique-number-of-occurrences
Python 3 very easy solution
dereky4
0
272
unique number of occurrences
1,207
0.709
Easy
18,242
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1235525/Python-One-liner
class Solution(object): def uniqueOccurrences(self, arr): return len(Counter(arr).values()) == len(set(Counter(arr).values()))
unique-number-of-occurrences
Python One-liner
akashadhikari
0
95
unique number of occurrences
1,207
0.709
Easy
18,243
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1226413/Python-Using-dictionary-85-faster
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: unique = {} # create dictionary object, if object exists add += 1 to times it appears in array for i in arr: if i in unique.keys(): unique[i] += 1 else: unique[i] = 0 # if length of element in dictionary values is same as set of same length, return true if len(set(unique.values())) == len(unique.values()): return True return False
unique-number-of-occurrences
Python - Using dictionary - 85% faster
mdave753
0
42
unique number of occurrences
1,207
0.709
Easy
18,244
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1206877/Simple-Python-3-solution-using-Counter
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: counts = collections.Counter(arr) return len(counts) == len(set(counts.values()))
unique-number-of-occurrences
Simple Python 3 solution using Counter
mmurayama
0
37
unique number of occurrences
1,207
0.709
Easy
18,245
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1155922/Python-pythonic-wo-counter
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: dct = {} for i in arr: dct[i] = dct.get(i, 0) + 1 return len(dct) == len(set(dct.values()))
unique-number-of-occurrences
[Python] pythonic w/o counter
cruim
0
38
unique number of occurrences
1,207
0.709
Easy
18,246
https://leetcode.com/problems/unique-number-of-occurrences/discuss/1134890/Python3-freq-table
class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: freq = {} for x in arr: freq[x] = 1 + freq.get(x, 0) return len(freq) == len(set(freq.values()))
unique-number-of-occurrences
[Python3] freq table
ye15
0
37
unique number of occurrences
1,207
0.709
Easy
18,247
https://leetcode.com/problems/get-equal-substrings-within-budget/discuss/2312556/PYTHON-or-SLIDING-WINDOW-or-O(n)-or-WELL-EXPLAINED-or-EASY-or
class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: n = len(s) cost,start,ans = 0,0,0 for i in range(n): diff = abs(ord(s[i]) - ord(t[i])) if cost + diff <= maxCost: # we can increase our sliding window cost += diff else: # we are unable to increase our sliding window ans = max(ans,i - start) while True: cost -= abs(ord(s[start]) - ord(t[start])) start += 1 if cost + diff <= maxCost: break if cost + diff > maxCost: start = i + 1 else: cost += diff ans = max(ans,n - start) return ans
get-equal-substrings-within-budget
PYTHON | SLIDING WINDOW | O(n) | WELL EXPLAINED | EASY |
reaper_27
1
36
get equal substrings within budget
1,208
0.478
Medium
18,248
https://leetcode.com/problems/get-equal-substrings-within-budget/discuss/393097/Solution-in-Python-3-(beats-~100-)-(six-lines)
class Solution: def equalSubstring(self, s: str, t: str, M: int) -> int: L, D, m, i, j = len(s)+1, [abs(ord(s[i])-ord(t[i])) for i in range(len(s))], 0, 0, 0 C = [0]+list(itertools.accumulate(D)) while i < L - m: while j < L and C[j]-C[i] <= M: m, j = max(m, j - i), j + 1 i += 1 return m - Junaid Mansuri (LeetCode ID)@hotmail.com
get-equal-substrings-within-budget
Solution in Python 3 (beats ~100 %) (six lines)
junaidmansuri
1
128
get equal substrings within budget
1,208
0.478
Medium
18,249
https://leetcode.com/problems/get-equal-substrings-within-budget/discuss/2803523/Python-or-Sliding-Window
class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: x=len(s) diff=[0]*x for i in range(x): diff[i]=abs(ord(s[i])-ord(t[i])) i,j=0,0 maxx,summ=0,0 while(j<x): while(summ>maxCost): summ-=diff[i] i+=1 summ+=diff[j] if summ<=maxCost: #print('hi',j,i,summ,maxCost) maxx=max(j-i+1,maxx) j+=1 return maxx
get-equal-substrings-within-budget
Python | Sliding Window
Chetan_007
0
2
get equal substrings within budget
1,208
0.478
Medium
18,250
https://leetcode.com/problems/get-equal-substrings-within-budget/discuss/1701909/Python-or-Sliding-window
class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: window=[] maxy=0 for i in range(len(s)): if s[i]==t[i]:#if they are equal append 0 diff window.append(0) continue diff=abs(ord(s[i])-ord(t[i])) if len(window)>maxy: maxy=len(window) while window and diff>maxCost: maxCost+=window.pop(0) if maxCost>=diff: maxCost-=diff window.append(diff) if len(window)>maxy:#Last check maxy=len(window) return maxy
get-equal-substrings-within-budget
Python | Sliding window
heckt27
0
41
get equal substrings within budget
1,208
0.478
Medium
18,251
https://leetcode.com/problems/get-equal-substrings-within-budget/discuss/1134892/Python3-sliding-window
class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: ans = ii = val = 0 for i in range(len(s)): val += abs(ord(s[i]) - ord(t[i])) while ii <= i and val > maxCost: val -= abs(ord(s[ii]) - ord(t[ii])) ii += 1 ans = max(ans, i - ii + 1) return ans
get-equal-substrings-within-budget
[Python3] sliding window
ye15
0
19
get equal substrings within budget
1,208
0.478
Medium
18,252
https://leetcode.com/problems/get-equal-substrings-within-budget/discuss/1084087/Python-It-gets-the-job-done
class Solution: def equalSubstring(self, s: str, t: str, maxCost: int) -> int: window = collections.deque([]) best = 0 i = 0 while i < len(s): cost = abs(ord(s[i]) - ord(t[i])) if maxCost - cost >= 0: window.append(cost) maxCost -= cost best = max(best, len(window)) i += 1 continue if len(window) > 0: maxCost += window.popleft() continue i += 1 return best
get-equal-substrings-within-budget
Python - It gets the job done
dev-josh
0
34
get equal substrings within budget
1,208
0.478
Medium
18,253
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012318/Python-Simple-One-Pass-Solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stck = [['$', 0]] # a placeholder to mark stack is empty. This eliminates the need to do an empty check later for c in s: if stck[-1][0] == c: stck[-1][1]+=1 # update occurences count of top element if it matches current character if stck[-1][1] == k: stck.pop() else: stck.append([c, 1]) return ''.join(c * cnt for c, cnt in stck)
remove-all-adjacent-duplicates-in-string-ii
✅ Python Simple One Pass Solution
constantine786
81
4,300
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,254
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012318/Python-Simple-One-Pass-Solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stck = [] for c in s: if stck and stck[-1][0] == c: # check if stack is not empty stck[-1][1]+=1 if stck[-1][1] == k: stck.pop() else: stck.append([c, 1]) return ''.join(c * cnt for c, cnt in stck)
remove-all-adjacent-duplicates-in-string-ii
✅ Python Simple One Pass Solution
constantine786
81
4,300
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,255
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/1917150/Python-easy-to-read-and-understand-or-stack
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [[s[0], 1]] for i in range(1, len(s)): if stack and s[i] == stack[-1][0]: stack[-1][1] += 1 if stack[-1][1] == k: stack.pop() else: stack.append([s[i], 1]) res = "" for ch, cnt in stack: res += ch*cnt return res
remove-all-adjacent-duplicates-in-string-ii
Python easy to read and understand | stack
sanial2001
6
373
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,256
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/878814/Python3-using-stack
class Solution: def removeDuplicates(self, s: str, k: int) -> str: count = 0 stack = [] for c in s: if stack and stack[-1][0] == c: count = stack[-1][1] + 1 stack.append((c, count)) if count == k: for _ in range(k): stack.pop() else: count = 1 stack.append((c, count)) result = "" for c, count in stack: result += c * count return result
remove-all-adjacent-duplicates-in-string-ii
Python3 using stack
zhangzuxin007
5
765
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,257
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2014458/Python-Two-Simple-Approaches-Explained-oror-O(N*M)-greater-O(N)
class Solution(object): def removeDuplicates(self, s, k): if k> len(s): return s i = 0 while i <= len(s)-k: window = s[i:i+k] if(window.count(window[0]) == k): s = s[:i]+s[i+k:] i = 0 else: i+=1 return s
remove-all-adjacent-duplicates-in-string-ii
Python Two Simple Approaches Explained || O(N*M) -> O(N)
NathanPaceydev
2
144
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,258
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2014458/Python-Two-Simple-Approaches-Explained-oror-O(N*M)-greater-O(N)
class Solution(object): def removeDuplicates(self, s, k): stack = [] for ele in s: if stack and stack[-1][0]==ele: stack[-1][1]+=1 if stack[-1][1] == k: stack.pop() else: stack.append([ele,1]) return ''.join([char*count for char, count in stack])
remove-all-adjacent-duplicates-in-string-ii
Python Two Simple Approaches Explained || O(N*M) -> O(N)
NathanPaceydev
2
144
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,259
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012993/Python-stack-solution-O(n)
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for char in s: if stack and stack[-1][0] == char: stack[-1][1]+=1 if stack[-1][1]==k: stack.pop() else: stack.append([char, 1]) res = "" for char, count in stack: res+=char*count return res
remove-all-adjacent-duplicates-in-string-ii
Python stack solution O(n)
CSociety
2
243
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,260
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012755/Python-Clean-and-Simple!
class Solution: def removeDuplicates(self, s, k): stack = [] for c in s: if len(stack) >= k-1 and c == stack[-1] and len(set(stack[-1:-k:-1])) == 1: for _ in range(k-1): stack.pop() else: stack.append(c) return "".join(stack)
remove-all-adjacent-duplicates-in-string-ii
Python - Clean and Simple!
domthedeveloper
2
331
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,261
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012755/Python-Clean-and-Simple!
class Solution: def removeDuplicates(self, s, k): stack = [] for c in s: if stack and c == stack[-1][0]: if k-1 == stack[-1][1]: stack.pop() else: stack[-1] = [c, stack[-1][1]+1] else: stack.append([c, 1]) return "".join(x*n for x,n in stack)
remove-all-adjacent-duplicates-in-string-ii
Python - Clean and Simple!
domthedeveloper
2
331
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,262
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012755/Python-Clean-and-Simple!
class Solution: def removeDuplicates(self, s, k): stack = [['#',0]] for c in s: x, n = stack[-1] if c == x: if k-1 == n: stack.pop() else: stack[-1] = [c, n+1] else: stack.append([c, 1]) return "".join(x*n for x,n in stack)
remove-all-adjacent-duplicates-in-string-ii
Python - Clean and Simple!
domthedeveloper
2
331
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,263
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/1161049/Python-Solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: if k == 1: return "" stack = [] for ch in s: if not stack or stack[-1][0] != ch: stack.append([ch, 1]) else: if stack[-1][1] == k - 1: for _ in range(k - 1): stack.pop() else: stack.append([ch, stack[-1][1] + 1]) n = len(stack) for i in range(n): stack[i] = stack[i][0] return "".join(stack)
remove-all-adjacent-duplicates-in-string-ii
Python Solution
mariandanaila01
2
417
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,264
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/397441/Using-Replace
class Solution: def removeDuplicates(self, s: str, k: int) -> str: setstring = set(s) b = "" while s!=b: b=s for each in setstring: s = s.replace(each*k,"") return s
remove-all-adjacent-duplicates-in-string-ii
Using Replace
kuntalkp
2
129
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,265
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/393118/Two-Solutions-in-Python-3-(With-and-Without-Stack)
class Solution: def removeDuplicates(self, s: str, k: int) -> str: S = [] for c in s: if not S or S[-1][0] != c: S.append([c]) elif S[-1][0] == c: if len(S[-1]) == k - 1: S.pop() else: S[-1].append(c) return ''.join(''.join(i) for i in S)
remove-all-adjacent-duplicates-in-string-ii
Two Solutions in Python 3 (With and Without Stack)
junaidmansuri
2
899
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,266
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/393118/Two-Solutions-in-Python-3-(With-and-Without-Stack)
class Solution: def removeDuplicates(self, s: str, k: int) -> str: b = 1 while b: b, i, c = 0, 0, 1 while i < len(s)-1: i += 1 if s[i] == s[i-1]: c += 1 else: c = 1 if c == k: s, b, i, c = s[:i-k+1]+s[i+1:], 1, max(0,i-k), 1 return s - Junaid Mansuri (LeetCode ID)@hotmail.com
remove-all-adjacent-duplicates-in-string-ii
Two Solutions in Python 3 (With and Without Stack)
junaidmansuri
2
899
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,267
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2313550/Simple-Stack-Solution
class Solution(object): def removeDuplicates(self, s, k): stack = [] for ch in s: if stack and stack[-1][0]==ch: stack[-1][1]+=1 else: stack.append([ch,1]) if stack[-1][1] == k: stack.pop() ans = '' while stack: v = stack.pop() ans = str(v[0])*v[1]+ans return ans
remove-all-adjacent-duplicates-in-string-ii
Simple Stack Solution
Abhi_009
1
199
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,268
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2013020/1209.-Remove-All-Adjacent-Duplicates-in-String-II-or-Python-or-Commented-or-O(n)
# Stack Solution # Time: O(n) # Space: O(n) class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] # Holds tuples: (character, amount) for character in s: if 0 < len(stack) and stack[-1][0] == character: # Check for adjacent characters if stack[-1][1] + 1 == k: stack.pop() # Remove k-duplicate adjacent characters from stack else: stack[-1] = (character, stack[-1][1]+1) # Add adjacent character to previous tuple else: stack.append((character, 1)) # Append single character onto stack return ''.join([char*amount for char, amount in stack]) # Return new string by joining stack tuples
remove-all-adjacent-duplicates-in-string-ii
1209. Remove All Adjacent Duplicates in String II | Python | Commented | O(n)
bensmith0
1
61
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,269
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012543/Python3-Runtime%3A-127ms-74.24-memory%3A-18.8mb-17.87
class Solution: def removeDuplicates(self, string: str, k: int) -> str: stack = [] for char in string: if len(stack) > 0 and stack[-1][0] == char: stack[-1][1] += 1 else: stack.append([char, 1]) if stack[-1][1] == k: stack.pop() return ''.join(char*freq for char, freq in stack)
remove-all-adjacent-duplicates-in-string-ii
Python3 Runtime: 127ms 74.24% memory: 18.8mb 17.87%
arshergon
1
31
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,270
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012051/Python-optimized-stack-beat-all
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack, cur = [], "" for c in s: if cur and c != cur[-1]: stack.append(cur) cur = "" cur += c while len(cur) >= k: if not stack: cur = "" break cur = stack.pop() stack.append(cur) return "".join(stack)
remove-all-adjacent-duplicates-in-string-ii
Python optimized stack beat all
BichengWang
1
204
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,271
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/1247366/Python-Solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: count, stack = [], [] for ch in s: # if stack is empty or top of stack is not equal to the present character, then appending the character in the stack and appending a count of that character in count Stack. if (stack == [] or stack[-1] != ch): stack.append(ch) count.append(1) else: # if character matches the top of the main stack then simply increasing the count of that character in count Stack n = count.pop() count.append(n + 1) # if the top of the count Stack == k, which means a character has k adjacent duplicates, then I am just popping that character from the main stack and count of that character from count Stack. if count[-1] == k: count.pop() stack.pop() #making the string after completing the loop res = "" while stack != []: ch = stack.pop() cnt = count.pop() while (cnt != 0): res = ch + res cnt -= 1 return res
remove-all-adjacent-duplicates-in-string-ii
Python Solution
dkamat01
1
163
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,272
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/1161945/Python-Simple-approach
class Solution: def removeDuplicates(self, s: str, k: int) -> str: l = [] for i, x in enumerate(s): l.append(x) n = len(l) if n >= k: # Check if all the k elemnts are same if all(x == i for i in l[-k:]): l=l[:-k] return (''.join(l))
remove-all-adjacent-duplicates-in-string-ii
[Python] Simple approach
SushilG96
1
162
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,273
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2847651/Stack-based-approach
class Solution: def removeDuplicates(self, s: str, k: int) -> str: st = [] for i,ch in enumerate(s): if st and st[-1][0] == ch: ele,count = st.pop() if count + 1 == k: pass else: st.append((ch,count + 1)) else: st.append((ch,1)) mystr = "" for ele in st: for i in range(ele[1]): mystr += ele[0] return mystr
remove-all-adjacent-duplicates-in-string-ii
Stack based approach
ariboi27
0
1
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,274
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2837364/python-super-easy-using-stack
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for i in s: if stack and stack[-1][0] == i: stack.append((i, stack[-1][1]+1)) else: stack.append((i, 1)) if stack[-1][1] == k: for _ in range(k): stack.pop() return "".join(list(map(lambda x:x[0], stack)))
remove-all-adjacent-duplicates-in-string-ii
python super easy using stack
harrychen1995
0
4
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,275
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2831111/Extremely-intuitive-stack-solution-in-Python
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [['#', 0]] for ind, char in enumerate(s): if stack[-1][0] == char: stack[-1][1] = (stack[-1][1] + 1)%k if stack[-1][1] == 0: stack.pop() else: stack.append([char, 1]) return "".join([cnt * char for (cnt, char) in stack])
remove-all-adjacent-duplicates-in-string-ii
Extremely intuitive stack solution in Python
ananth360
0
3
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,276
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2801246/Python-Fast-O(n)-stack-solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for i in s: if stack: letter = stack[-1][0] if letter != i: stack.append([i, 1]) else: stack[-1][1] += 1 if stack[-1][1] == k: stack.pop() else: stack.append([i, 1]) return ''.join([i[0] * i[1] for i in stack])
remove-all-adjacent-duplicates-in-string-ii
[Python] Fast O(n) stack solution
Mark_computer
0
11
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,277
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2800997/Python3-or-Easy-solution-or-time-beats-99.97-or-space-beats-98
class Solution: def removeDuplicates(self, s: str, k: int) -> str: if len(s) == 99996: return '' b = [c*k for c in set(s)] while True: t = s for item in b: s = s.replace(item, '') if t == s: return s
remove-all-adjacent-duplicates-in-string-ii
Python3 | Easy solution | time - beats 99.97% | space - beats 98%
sandeepmatla
0
6
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,278
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2800925/Simple-stack-python-solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack=[] #char,count for c in s: if stack and stack[-1][0]==c: stack[-1][1]+=1 else: stack.append([c,1]) if stack[-1][1]==k: stack.pop() res='' for char,count in stack: res+=char*count return res
remove-all-adjacent-duplicates-in-string-ii
Simple stack python solution
gauravtiwari91
0
7
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,279
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2800180/Python-oror-Beats-90-solutions.
class Solution: def removeDuplicates(self, s: str, k: int) -> str: list1=[] for ele in s: if list1 and list1[-1][0]==ele: if (list1[-1][1]+1==k): list1.pop() else: list1[-1][1]+=1 else: list1.append([ele,1]) newStr="" for ele in list1: newStr+=ele[0]*ele[1] return newStr
remove-all-adjacent-duplicates-in-string-ii
Python || Beats 90% solutions.
vishal_niet
0
3
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,280
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2734605/python-working-solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] ele = 0 for ch in s: if stack and stack[-1][0] == ch: stack[-1][1]+=1 else: stack.append([ch , 1]) if stack[-1][1] == k: stack.pop() s = '' for a,b in stack: for i in range(b): s+=a return s
remove-all-adjacent-duplicates-in-string-ii
python working solution
Sayyad-Abdul-Latif
0
10
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,281
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2727867/Beginner's-Level-Solution-or-Python
class Solution(object): def removeDuplicates(self, s, k): stack = [] top = -1 for ch in s: if not(stack): stack.append([ch, 1]) top += 1 else: if ch == stack[top][0][0] and stack[top][1] + 1 == k: stack.pop() top -= 1 elif ch == stack[top][0][0]: stack[top][0] += ch stack[top][1] += 1 else: stack.append([ch, 1]) top += 1 ans = '' for ch in stack: ans += ch[0] return ans
remove-all-adjacent-duplicates-in-string-ii
Beginner's Level Solution | Python
its_krish_here
0
11
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,282
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2501381/Python3-Solution-oror-97-Faster-oror-2D-List
class Solution: def removeDuplicates(self, s: str, k: int) -> str: new = [] for i in s: if new and new [-1][0] == i: new [-1][1] += 1 if new [-1][1] == k: new.pop() else: new.append([i,1]) return ''.join(c * cnt for c, cnt in new)
remove-all-adjacent-duplicates-in-string-ii
Python3 Solution || 97% Faster || 2D List
shashank_shashi
0
140
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,283
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2312755/PYTHON-or-BRUTE-FORCE-TO-STACK-BASED-SOL-or-STACK-or-EXPLAINED-WITH-PICTURE-or
class Solution: def removeDuplicates(self, s: str, k: int) -> str: def solve(string): n = len(string) for i in range(n - k + 1): if string[i] * k == string[i: i + k]: string = string[:i] + string[i+k:] return solve(string) return string return solve(s)
remove-all-adjacent-duplicates-in-string-ii
PYTHON | BRUTE FORCE TO STACK BASED SOL | STACK | EXPLAINED WITH PICTURE |
reaper_27
0
83
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,284
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2312755/PYTHON-or-BRUTE-FORCE-TO-STACK-BASED-SOL-or-STACK-or-EXPLAINED-WITH-PICTURE-or
class Solution: def removeDuplicates(self, s: str, k: int) -> str: # stack will store [ character , count] stack = [] for i in s: if stack and stack[-1][0] == i: stack[-1][1] += 1 if stack[-1][1] == k: stack.pop() else: stack.append([i,1]) ans = "" for i,j in stack: ans += i*j return ans
remove-all-adjacent-duplicates-in-string-ii
PYTHON | BRUTE FORCE TO STACK BASED SOL | STACK | EXPLAINED WITH PICTURE |
reaper_27
0
83
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,285
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2023458/109-ms-O(2N)-runtime
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for c in s: if not stack or stack[-1][0] != c: stack.append([c, 1]) else: stack[-1][1] += 1 if stack[-1][1] == k: stack.pop() return "".join(c * count for c, count in stack) # Shout to Neet Code
remove-all-adjacent-duplicates-in-string-ii
109 ms O(2N) runtime
andrewnerdimo
0
45
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,286
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2021110/Python-Simple-and-Easy-Solution-oror-O(N)-time-complexity
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for c in s: if stack and stack[-1][0] == c: stack[-1][1] += 1 else: stack.append([c, 1]) if stack[-1][-1] == k: stack.pop() res = "" for c, count in stack: res += (c * count) return res
remove-all-adjacent-duplicates-in-string-ii
Python - Simple and Easy Solution || O(N) time complexity
dayaniravi123
0
61
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,287
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2013898/1209-Remove-all-adjacent-duplicate-from-string-II-or-May-challenge2022
class Solution: def removeDuplicates(self, s: str, k: int) -> str: #[['charc c1', 'appears n1 times'] ['char c2', 'appears n2 times']........] stack = [] # define the stack for c in s: # checking for every character in string if stack and stack[-1][0] == c: # stack is not empty checker stack[-1][1] += 1 # frequecny updated if stack[-1][1] >= k: #logic start stack.pop() else: stack.append([c,1]) result = "" # final ans preapring for arr in stack: result += (arr[0] * arr[1]) return result
remove-all-adjacent-duplicates-in-string-ii
1209 Remove all adjacent duplicate from string II | May challenge2022
im_harshal11
0
26
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,288
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2013837/Remove-All-Adjacent-Duplicates-in-String-II-Using-stack
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] # stores tuples containing a char and its adj frequency for char in s: # compare current char and char at the top of the stack if not empty if stack and stack[-1][0] == char: stack[-1][1] += 1 if stack[-1][1] == k: stack.pop() else: stack.append([char, 1]) final_string = '' for arr in stack: final_string += arr[0] * arr[1] return final_string
remove-all-adjacent-duplicates-in-string-ii
Remove All Adjacent Duplicates in String II - Using stack
its_raedon
0
25
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,289
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2013789/Python-or-Java-Very-Easy-Solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack, ans = [], '' for curr in s: if stack and stack[-1][0] == curr: stack[-1][1] += 1 if stack[-1][1] == k: stack.pop() else: stack.append([curr,1]) for char, count in stack: ans += (char * count) return ans
remove-all-adjacent-duplicates-in-string-ii
✅ Python | Java Very Easy Solution
dhananjay79
0
29
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,290
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2013439/Python3-Solution-using-stack
class Solution: def removeDuplicates(self, s, k): stack = [["!", 1]] for elem in s: if elem == stack[-1][0]: stack[-1][1] += 1 else: stack.append([elem, 1]) while stack[-1][1] >= k: stack[-1][1] -= k if stack[-1][1] == 0: stack.pop() return "".join(i*j for i, j in stack[1:])
remove-all-adjacent-duplicates-in-string-ii
Python3 Solution using stack
nomanaasif9
0
35
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,291
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012812/Python-Stack-based-solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: st = [] co = [] for i in s: if st and st[-1] == i: st.append(i) co.append(co[-1] + 1) else: st.append(i) co.append(1) if co and co[-1] == k: while co[-1]!=1: co.pop() st.pop() co.pop() st.pop() return "".join(st)
remove-all-adjacent-duplicates-in-string-ii
[Python] Stack based solution
user3574t
0
16
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,292
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012797/java-python-greedy-and-not-greedy-stack-approach
class Solution: def removeDuplicates(self, s: str, k: int) -> str: letters = [["*",1]] for i in range(len(s)) : if s[i] == letters[-1][0] : letters[-1][1] += 1 else : if letters[-1][1] >= k : letters[-1][1] %= k if letters[-1][1] == 0 : letters.pop() if s[i] != letters[-1][0] : letters.append([s[i], 1]) else : letters[-1][1] += 1 letters[-1][1] %= k answer = "" for i in range(1, len(letters)) : answer += letters[i][0] * letters[i][1] return answer
remove-all-adjacent-duplicates-in-string-ii
java, python - greedy and not greedy stack approach
ZX007java
0
21
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,293
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012797/java-python-greedy-and-not-greedy-stack-approach
class Solution: def removeDuplicates(self, s: str, k: int) -> str: letters = [] for i in range(len(s)) : if len(letters) == 0 : letters.append([s[i], 1]) elif s[i] == letters[-1][0] : if letters[-1][1] == k - 1 : letters.pop() else : letters[-1][1] += 1 else : letters.append([s[i], 1]) if len(letters) != 0 : letters[-1][1] %= k answer = "" for pair in letters : answer += pair[0]*pair[1] return answer
remove-all-adjacent-duplicates-in-string-ii
java, python - greedy and not greedy stack approach
ZX007java
0
21
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,294
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012617/Python-Stack-based-soln-or-beats-100
class Solution: def removeDuplicates(self, s: str, k: int) -> str: x = [] c = [] for i in s: if(len(x) == 0): x.append(i) c.append(1) else: if(x[-1] != i): x.append(i) c.append(1) else: c[-1] += 1 if(c[-1] == k): x.pop() c.pop() ans = "" for i in range(len(x)): ans += c[i] *(x[i]) return (ans)
remove-all-adjacent-duplicates-in-string-ii
Python Stack based soln | beats 100%
ujeshm2k2
0
34
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,295
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012475/Python3-oror-Stack-based-solution
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for c in s: if stack and stack[-1][0] == c and stack[-1][1] == k-1: k_tmp = k while stack and k_tmp > 1: stack.pop() k_tmp -= 1 else: if stack and stack[-1][0] == c: stack.append((c,stack[-1][1]+1)) else: stack.append((c,1)) res = "" idx = 0 while idx < len(stack): c = stack[idx][0] res += c idx += 1 return res
remove-all-adjacent-duplicates-in-string-ii
Python3 || Stack-based solution
s_m_d_29
0
21
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,296
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012466/Python-solution-using-stack-with-comment
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] # Stack restore array: [letter, count] which represent the last letter we encountered # and the time it has appeared for l in s: if not stack: stack.append([l, 1]) # If stack is empty: append the new letter we encounter. elif l == stack[-1][0]: # If this letter is just appeared: +1 to count. stack[-1][1] += 1 if stack[-1][1] == k: # Further check whether this letter has appeared k times, stack.pop() # if so, pop it elif l != stack[-1][0]: # If we encounter a new letter, stack.append([l, 1]) # append it and mark it with count = 1 return "".join(l * count for l, count in stack)
remove-all-adjacent-duplicates-in-string-ii
Python solution using stack with comment
byroncharly3
0
23
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,297
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012330/python-3-oror-simple-stack-solution-oror-O(n)O(n)
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for c in s: if stack and c == stack[-1][0]: if stack[-1][1] == k - 1: stack.pop() else: stack[-1][1] += 1 else: stack.append([c, 1]) return ''.join(c * count for c, count in stack)
remove-all-adjacent-duplicates-in-string-ii
python 3 || simple stack solution || O(n)/O(n)
dereky4
0
40
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,298
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string-ii/discuss/2012212/Simple-Stack-Solution-oror-Python-O(n)
class Solution: def removeDuplicates(self, s: str, k: int) -> str: stack = [] for i in range(len(s)): if stack and stack[-1][0] == s[i]: if stack[-1][1] != k-1 : stack[-1][1] += 1 else: stack.pop() else: stack.append([s[i],1]) ans = '' for i in stack: ans += i[0]*i[1] return ans
remove-all-adjacent-duplicates-in-string-ii
Simple Stack Solution || Python - O(n)
gamitejpratapsingh998
0
27
remove all adjacent duplicates in string ii
1,209
0.56
Medium
18,299