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/last-stone-weight/discuss/1740567/Python3Python-Standard-Max-Heap-Solution | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
max_heap = []
for stone in stones:
heapq.heappush(max_heap, -stone)
while len(max_heap) > 1:
stone_1 = -heapq.heappop(max_heap)
stone_2 = -heapq.heappop(max_heap)
rest = stone_1 - stone_2
if rest == 0:
continue
else:
heapq.heappush(max_heap, -rest)
if max_heap:
return -max_heap[0]
else:
return 0 | last-stone-weight | [Python3/Python] Standard Max Heap Solution | Mikey98 | 1 | 91 | last stone weight | 1,046 | 0.647 | Easy | 17,000 |
https://leetcode.com/problems/last-stone-weight/discuss/1143513/Python-using-heapq | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
heap = list()
for stone in stones:
heapq.heappush(heap, (-stone, stone))
while len(heap) > 1:
y = heapq.heappop(heap)[1]
x = heapq.heappop(heap)[1]
if x != y:
heapq.heappush(heap, (-(y-x), y-x))
return heap[0][1] if heap else 0 | last-stone-weight | Python using heapq | keewook2 | 1 | 173 | last stone weight | 1,046 | 0.647 | Easy | 17,001 |
https://leetcode.com/problems/last-stone-weight/discuss/1099583/WEEB-DOES-GREEDY-PYTHON-FASTER-THAN-87.06 | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
while len(stones)>1:
stones.sort()
diff = stones[-1] - stones[-2]
if diff == 0:
stones.pop()
stones.pop()
else:
stones.pop()
stones.pop()
stones.append(diff)
return stones[0] if stones else 0 | last-stone-weight | WEEB DOES GREEDY PYTHON FASTER THAN 87.06% | Skywalker5423 | 1 | 107 | last stone weight | 1,046 | 0.647 | Easy | 17,002 |
https://leetcode.com/problems/last-stone-weight/discuss/2838089/Easy-and-faster-than-92-python-solution | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
# Intiliaze max heap
max_heap = []
# Convert array into max heap
for stone in stones:
heappush(max_heap,-stone)
while len(max_heap)>1:
# get largest and second largest element from max heap
largest_stone = -1 * heappop(max_heap)
second_largest_stone = -1 * heappop(max_heap)
# push largest stone - second largest stone
if largest_stone != second_largest_stone:
heappush(max_heap, -(largest_stone-second_largest_stone))
# return first element of max heap
if max_heap:
return -max_heap[0]
# if no element in max heap
return 0 | last-stone-weight | Easy and faster than 92% python solution | IronmanX | 0 | 2 | last stone weight | 1,046 | 0.647 | Easy | 17,003 |
https://leetcode.com/problems/last-stone-weight/discuss/2837526/Simple-Python-solution-suing-list-reverse-sorting | class Solution:
def lastStoneWeight(self, stones):
stones = sorted(stones, reverse=True)
while len(stones) > 1:
print(stones)
sub = abs(stones[0] - stones[1])
if sub != 0:
stones.append(sub)
stones.pop(1)
stones.pop(0)
stones = sorted(stones, reverse=True)
if stones == []:
return 0
return stones[0] | last-stone-weight | Simple Python solution suing list reverse sorting | youssefyzahran | 0 | 1 | last stone weight | 1,046 | 0.647 | Easy | 17,004 |
https://leetcode.com/problems/last-stone-weight/discuss/2830506/Last-Stone-Weight-or-Python-Solution-or-Using-Heap | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
for i, val in enumerate(stones):
stones[i] = -val
heapify(stones)
while stones:
s1 = -heappop(stones)
if not stones:
return s1
s2 = -heappop(stones)
if s1 > s2:
heappush(stones, s2-s1)
return 0 | last-stone-weight | Last Stone Weight | Python Solution | Using Heap | nishanrahman1994 | 0 | 3 | last stone weight | 1,046 | 0.647 | Easy | 17,005 |
https://leetcode.com/problems/last-stone-weight/discuss/2797695/Simple-python3-sorted-list-solution-beats-95 | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
while 1 < len(stones):
stones = sorted(stones, reverse=True)
if stones[0] == stones[1]:
del stones[0]
del stones[0]
else:
stones[0] -= stones[1]
del stones[1]
if len(stones) == 1:
return stones[0]
else:
return 0 | last-stone-weight | 🤫 Simple python3 sorted list solution beats 95% | isaevartem | 0 | 2 | last stone weight | 1,046 | 0.647 | Easy | 17,006 |
https://leetcode.com/problems/last-stone-weight/discuss/2773837/Easy-python-solution-using-stack | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
while len(stones)>1:
stones=sorted(stones)[::-1]
a,b=stones[0],stones[1]
if(a==b):
stones.pop(0)
stones.pop(0)
elif(a!=b):
stones[0]-=stones[1]
stones.pop(1)
print(stones)
if len(stones)==0:
return 0
else:
return stones[0] | last-stone-weight | Easy python solution using stack | liontech_123 | 0 | 2 | last stone weight | 1,046 | 0.647 | Easy | 17,007 |
https://leetcode.com/problems/last-stone-weight/discuss/2683036/Clean-Self-explanatory-python-code-O(n-*-logn)-Time-complexity | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
stones = [-1*stone for stone in stones]
heapq.heapify(stones)
while len(stones) > 1:
first = -1 * heapq.heappop(stones)
second = -1 * heapq.heappop(stones)
if first != second:
newStone = first - second
heapq.heappush(stones, -1 * newStone)
if len(stones) == 1:
return -1 * stones[0]
else:
return 0 | last-stone-weight | Clean Self explanatory python code O(n * logn) Time complexity | Furat | 0 | 15 | last stone weight | 1,046 | 0.647 | Easy | 17,008 |
https://leetcode.com/problems/last-stone-weight/discuss/2645351/Python-Solution-oror-Using-Heap-with-explanation. | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
for i in range(len(stones)):
stones[i] = -1*stones[i]
heapq.heapify(stones)
while len(stones)!=1:
a = -1 * heapq.heappop(stones)
b = -1 * heapq.heappop(stones)
res = a-b
heapq.heappush(stones,-1*res)
return -1*stones[0] | last-stone-weight | Python Solution || Using Heap with explanation. | aryan_codes_here | 0 | 4 | last stone weight | 1,046 | 0.647 | Easy | 17,009 |
https://leetcode.com/problems/last-stone-weight/discuss/2578195/python-short-solution-using-heap | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
stones=[-i for i in stones]
heapq.heapify(stones)
while stones and len(stones)>1:
large1=-1*heapq.heappop(stones)
large2=-1*heapq.heappop(stones)
diff=-1*(large1-large2)
if diff:
heapq.heappush(stones,diff)
return -stones[0] if stones else 0 | last-stone-weight | python short solution using heap | benon | 0 | 39 | last stone weight | 1,046 | 0.647 | Easy | 17,010 |
https://leetcode.com/problems/last-stone-weight/discuss/2509086/SIMPLEST-SOLUTION-Runtime%3A-60-ms-faster-than-21.67-Memory%3A-13.9-MB-less-than-14.37 | class Solution:
def help(self,x,y):
if x==y:
return 0
else:
return y-x
def lastStoneWeight(self, stones: List[int]) -> int:
while len(stones)!=1 and len(stones)!=0:
stones.sort()
z=self.help(stones[-2],stones[-1])
stones.pop()
stones.pop()
stones.append(z)
if len(stones)==1:
return stones[0]
return | last-stone-weight | SIMPLEST SOLUTION Runtime: 60 ms, faster than 21.67% Memory: 13.9 MB, less than 14.37% | DG-Problemsolver | 0 | 14 | last stone weight | 1,046 | 0.647 | Easy | 17,011 |
https://leetcode.com/problems/last-stone-weight/discuss/2463328/python3-97-heap-solution | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
stones = [-stone for stone in stones ]
heapq.heapify(stones)
while len(stones)!= 1:
heapq.heappush(stones,-abs(heapq.heappop(stones) - heapq.heappop(stones)))
return -stones[0] | last-stone-weight | python3 97% heap solution | 1ncu804u | 0 | 31 | last stone weight | 1,046 | 0.647 | Easy | 17,012 |
https://leetcode.com/problems/last-stone-weight/discuss/2463226/Python-3-Optimal-Solution-with-explanation | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
stones = [-s for s in stones]
heapq.heapify(stones)
while len(stones) > 1:
first = heapq.heappop(stones)
second = heapq.heappop(stones)
if second > first:
heapq.heappush(stones, first - second)
stones.append(0)
return abs(stones[0]) | last-stone-weight | [Python 3] Optimal Solution with explanation | WhiteBeardPirate | 0 | 21 | last stone weight | 1,046 | 0.647 | Easy | 17,013 |
https://leetcode.com/problems/last-stone-weight/discuss/2390296/Python-simple-solution-or-heap-or-priority_queue | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
stones = [-x for x in stones]
heapq.heapify(stones)
while len(stones) > 1:
mx1 = -heapq.heappop(stones)
mx2 = -heapq.heappop(stones)
if mx1 - mx2:
heapq.heappush(stones, -(mx1 - mx2))
if len(stones):
return -heapq.heappop(stones)
return 0 | last-stone-weight | Python simple solution | heap | priority_queue | wilspi | 0 | 84 | last stone weight | 1,046 | 0.647 | Easy | 17,014 |
https://leetcode.com/problems/last-stone-weight/discuss/2369494/python-basic-solution-O(n*n*logn) | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
while len(stones) > 1:
stones.sort()
x = stones[len(stones) - 1]
y = stones[len(stones) - 2]
stones.pop()
stones.pop()
if x - y != 0:
stones.append(x - y)
if len(stones) == 0:
return 0
return stones[0] | last-stone-weight | python basic-solution O(n*n*logn) | akashp2001 | 0 | 32 | last stone weight | 1,046 | 0.647 | Easy | 17,015 |
https://leetcode.com/problems/last-stone-weight/discuss/2335752/python3-beginner-friendly-iterative-solution-with-explanation | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
stones = sorted(stones)
while stones: #here we remove all the 0 weight stones in list
if stones[0] == 0:
stones.remove(0)
else: break # for the cases that len(stones) == 0 and stones[0] != 1, break to next step
if len(stones) == 0: #cases that the result is 0
return 0
elif len(stones) == 1: #cases that only 1 stone remains
return stones[0]
else: # here's the main lines to conduct what the problem demands.
maxstone, secstone = stones[-1], stones[-2]
stones[-1] = maxstone - secstone
stones[-2] = 0
return self.lastStoneWeight(stones) #iter here so we will keep conduct our lines to the result we want | last-stone-weight | [python3] beginner friendly iterative solution with explanation | AustinHuang823 | 0 | 21 | last stone weight | 1,046 | 0.647 | Easy | 17,016 |
https://leetcode.com/problems/last-stone-weight/discuss/2238102/Python3-Simple-Solution | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
while len(stones) > 1:
l1 = max(stones)
stones.remove(l1)
l2 = max(stones)
stones.remove(l2)
temp = abs(l1 - l2)
if(temp > 0):
stones.append(temp)
# print(stones)
if stones == []:
return 0
else:
return stones[0] | last-stone-weight | Python3 Simple Solution | irapandey | 0 | 33 | last stone weight | 1,046 | 0.647 | Easy | 17,017 |
https://leetcode.com/problems/last-stone-weight/discuss/2227723/Python-or-Easy-solution-using-sorting | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
while len(stones) >= 2:
stones.sort()
y = stones.pop() # most heavier
x = stones.pop() # second most heavier
stones.append(y-x)
return stones[0] | last-stone-weight | Python | Easy solution using sorting | __Asrar | 0 | 15 | last stone weight | 1,046 | 0.647 | Easy | 17,018 |
https://leetcode.com/problems/last-stone-weight/discuss/2156386/Simple-python-beginner-solution | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
while True:
if len(stones) == 1:
return stones[0]
stones = sorted(stones, reverse=True)
stones.append(stones[0]-stones[1])
del stones[0:2] | last-stone-weight | Simple python beginner solution | pro6igy | 0 | 35 | last stone weight | 1,046 | 0.647 | Easy | 17,019 |
https://leetcode.com/problems/last-stone-weight/discuss/2154084/Python-Simple-solution-using-Heap | class Solution:
# Time - O(N. Log N)
def lastStoneWeight(self, stones: List[int]) -> int:
# For max heap, we negate the elements
stones = [-stone for stone in stones]
heapq.heapify(stones)
while len(stones) > 1:
x = heapq.heappop(stones)
y = heapq.heappop(stones)
if x < y:
heapq.heappush(stones, -(y - x))
return abs(stones[0]) if len(stones) == 1 else 0 | last-stone-weight | [Python] Simple solution using Heap | Gp05 | 0 | 24 | last stone weight | 1,046 | 0.647 | Easy | 17,020 |
https://leetcode.com/problems/last-stone-weight/discuss/1961695/Python-solution-using-minimum-heap | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
stones = [-s for s in stones]
heapq.heapify(stones)
while len(stones) > 1:
first = heapq.heappop(stones)
second = heapq.heappop(stones)
if second > first:
heapq.heappush(stones, first - second)
stones.append(0)
return abs(stones[0]) | last-stone-weight | Python solution using minimum heap | nikhitamore | 0 | 27 | last stone weight | 1,046 | 0.647 | Easy | 17,021 |
https://leetcode.com/problems/last-stone-weight/discuss/1944186/python-3-oror-simple-heap-solution | class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
for i, stone in enumerate(stones):
stones[i] = -stone
heapq.heapify(stones)
while len(stones) > 1:
y, x = heapq.heappop(stones), heapq.heappop(stones)
if x != y:
heapq.heappush(stones, y - x)
return -stones[0] if stones else 0 | last-stone-weight | python 3 || simple heap solution | dereky4 | 0 | 52 | last stone weight | 1,046 | 0.647 | Easy | 17,022 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/1291694/Easy-Python-Solution(89.05) | class Solution:
def removeDuplicates(self, s: str) -> str:
stack=[s[0]]
for i in range(1,len(s)):
if(stack and stack[-1]==s[i]):
stack.pop()
else:
stack.append(s[i])
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Easy Python Solution(89.05%) | Sneh17029 | 11 | 807 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,023 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2798255/fastest-solution-possibles-in-python | class Solution:
def removeDuplicates(self, s: str) -> str:
ans=[]
for a in s:
if(len(ans)>0 and ans[-1]==a):
ans.pop()
else:
ans.append(a)
return("".join(ans)) | remove-all-adjacent-duplicates-in-string | fastest solution possibles in python | droj | 6 | 1,100 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,024 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/1158365/Python-Stack-Solution-95.37-Faster | class Solution:
def removeDuplicates(self, S: str) -> str:
stack = []
for s in S:
if stack and stack[-1] == s:
stack.pop()
else:
stack.append(s)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Python Stack Solution 95.37% Faster | khushisrivastava53 | 3 | 158 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,025 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2725400/Python3ororO(N)ororRuntime-104-ms-Beats-82.48-Memory-14.7-MB-Beats-86.13 | class Solution:
def removeDuplicates(self, s: str) -> str:
ans = []
i = 0
for i in s:
if len(ans) > 0:
if i == ans[-1]:
ans.pop()
else:
ans.append(i)
else:
ans.append(i)
return ''.join(ans) | remove-all-adjacent-duplicates-in-string | Python3||O(N)||Runtime 104 ms Beats 82.48% Memory 14.7 MB Beats 86.13% | Sneh713 | 2 | 172 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,026 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2651043/Easy-Python-solution-using-if-else-with-explanation | class Solution:
def removeDuplicates(self, s: str) -> str:
# Initiate a blank list
str_list = []
for i in s:
# Remove the element from that list only if its Non Empty and its last appended element = current element
if str_list and str_list[-1] == i:
str_list.pop()
else:
# Else, keep appending the characters
str_list.append(i)
return ''.join(str_list) | remove-all-adjacent-duplicates-in-string | Easy Python solution using if-else with explanation | code_snow | 2 | 116 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,027 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2012553/Python-Stack-%2B-Two-Pointer-Multiple-Solutions!-Clean-and-Simple | class Solution:
def removeDuplicates(self, s):
for i,(a,b) in enumerate(pairwise(s)):
if a == b: return self.removeDuplicates(s[:i] + s[i+2:])
return s | remove-all-adjacent-duplicates-in-string | Python - Stack + Two-Pointer - Multiple Solutions! Clean and Simple | domthedeveloper | 2 | 102 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,028 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2012553/Python-Stack-%2B-Two-Pointer-Multiple-Solutions!-Clean-and-Simple | class Solution:
def removeDuplicates(self, s):
stack = []
for c in s:
if stack and c == stack[-1]: stack.pop()
else: stack.append(c)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Python - Stack + Two-Pointer - Multiple Solutions! Clean and Simple | domthedeveloper | 2 | 102 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,029 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2012553/Python-Stack-%2B-Two-Pointer-Multiple-Solutions!-Clean-and-Simple | class Solution:
def removeDuplicates(self, s):
lst, n = list(s), len(s)
l, r = 0, 1
while r < n:
if l >= 0 and lst[l] == lst[r]:
l -= 1
r += 1
else:
l += 1
lst[l] = lst[r]
r += 1
return "".join(lst[:l+1]) | remove-all-adjacent-duplicates-in-string | Python - Stack + Two-Pointer - Multiple Solutions! Clean and Simple | domthedeveloper | 2 | 102 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,030 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/1974989/Easiest-and-Simplest-Python3-Solution-or-Application-Of-Stack-or-O(n)-or-Beginner-friendly | class Solution:
def removeDuplicates(self, s: str) -> str:
temp=[]
for i in range(len(s)):
if temp!=[] and temp[-1]==s[i] and i>0:
temp.pop()
else:
temp.append(s[i])
return "".join(temp) | remove-all-adjacent-duplicates-in-string | Easiest & Simplest Python3 Solution | Application Of Stack | O(n) | Beginner-friendly | RatnaPriya | 2 | 148 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,031 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/1566937/Very-simple-Python-stack-solution | class Solution:
def removeDuplicates(self, s: str) -> str:
p,stack=0,[]
while p<len(s):
currLetter=s[p]
if stack:
lastLetter=stack.pop()
if currLetter!=lastLetter : #if not = add both, else dont add anything
stack.append(lastLetter)
stack.append(currLetter)
else: #if stack is empty put
stack.append(currLetter)
p+=1
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Very simple Python 🐍 stack solution | InjySarhan | 2 | 151 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,032 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2801124/Python-the-easiest-quick-solution-T96 | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = [""]
for c in s:
if stack[-1] == c:
stack.pop()
else:
stack.append(c)
return ''.join(stack) | remove-all-adjacent-duplicates-in-string | Python the easiest quick solution T96% | AgentIvan | 1 | 21 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,033 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800402/Python-Simple-Python-Solution-73-ms-faster-than-95.50 | class Solution:
def removeDuplicates(self, s: str) -> str:
stack=[]
for i in s:
if stack and stack[-1]==i:
stack.pop()
else:
stack.append(i)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | [ Python ] 🐍🐍 Simple Python Solution ✅✅73 ms, faster than 95.50% | sourav638 | 1 | 7 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,034 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2799009/Python-oror-Easy-oror-Stack-oror-O(N)-Solution | class Solution:
def removeDuplicates(self, s: str) -> str:
a,n=[],len(s)
for i in range(n):
if len(a)!=0 and a[-1]==s[i]:
a.pop()
else:
a.append(s[i])
return ''.join(a) | remove-all-adjacent-duplicates-in-string | Python || Easy || Stack || O(N) Solution | DareDevil_007 | 1 | 14 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,035 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2798419/Detailed-Explanation-of-the-Python-Solution-or-O(N)-TC-or-99-Faster | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for letter in s:
if stack and stack[-1] == letter:
stack.pop()
else:
stack.append(letter)
return ''.join(stack) | remove-all-adjacent-duplicates-in-string | ✔️ Detailed Explanation of the Python Solution | O(N) TC | 99% Faster 🔥 | pniraj657 | 1 | 63 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,036 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2798182/Simple-Python-Solution-or-Faster-than-96.47-or-Stack-Implementation | class Solution:
def removeDuplicates(self, s: str) -> str:
st = []
for i in s:
if st and i == st[-1]:
st.pop()
else:
st.append(i)
return "".join(st) | remove-all-adjacent-duplicates-in-string | Simple Python Solution | Faster than 96.47% | Stack Implementation | aniketbhamani | 1 | 5 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,037 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2798077/Python3-easy-solution1047.-Remove-All-Adjacent-Duplicates-In-String | class Solution:
def removeDuplicates(self, s: str) -> str:
a=[]
for letter in s:
a.append(letter)
if len(a)>1 and a[-1] == a[-2]:
a.pop()
a.pop()
return "".join(a) | remove-all-adjacent-duplicates-in-string | Python3 easy solution1047. Remove All Adjacent Duplicates In String | shivayadav2k03 | 1 | 5 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,038 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2798059/Easiest-Solution-oror-One-Pass-oror-O(1)-memory | class Solution:
def removeDuplicates(self, s: str) -> str:
idx =0
while(idx+1<len(s)):
if(s[idx]==s[idx+1]):
s= s[:idx]+s[idx+2:]
if idx > 0:
idx -= 1
else:
idx += 1
return s | remove-all-adjacent-duplicates-in-string | Easiest Solution || One Pass || O(1) memory | hasan2599 | 1 | 65 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,039 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2175624/Python3-O(n)-oror-O(n)-Runtime%3A-124ms-47.66-memory%3A-14.8mb-84.49 | class Solution:
# O(n) || O(n)
# Runtime: 124ms 47.66% memory: 14.8mb 84.49%
def removeDuplicates(self, string: str) -> str:
stack = []
for i in string:
change = False
while stack and stack[-1] == i:
stack.pop()
change = True
if not change:
stack.append(i)
return ''.join(stack) | remove-all-adjacent-duplicates-in-string | Python3 O(n) || O(n) Runtime: 124ms 47.66% memory: 14.8mb 84.49% | arshergon | 1 | 106 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,040 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/1450287/Python3Python-Simple-solution-in-O(n)-w-comments | class Solution:
def removeDuplicates(self, s: str) -> str:
# Init
idx = 0
n = len(s)-1
# While index is in range
while idx < n:
# Check if curr char and next
# char is same, if yes, split the string
if s[idx] == s[idx+1]:
# Get substring till current index,
# append with substring two steps from
# currrent index
s = s[:idx] + s[idx+2:]
# Decrement index if not zero
idx = idx - 1 if idx else 0
# Decrement lenght of string by 2 chars
n -= 2
else: # Increament current index
idx += 1
return s | remove-all-adjacent-duplicates-in-string | [Python3/Python] Simple solution in O(n) w/ comments | ssshukla26 | 1 | 119 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,041 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/1434594/One-pass-92-speed | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for c in s:
if stack and stack[-1] == c:
stack.pop()
else:
stack.append(c)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | One pass, 92% speed | EvgenySH | 1 | 226 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,042 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/1304186/Easy-Python-Solution-using-Stack | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for ch in s:
if stack and stack[-1] == ch:
stack.pop()
else:
stack.append(ch)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Easy Python Solution using Stack | MihailP | 1 | 185 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,043 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/1126077/Python-Stacks-(Easy-Approach) | class Solution:
def removeDuplicates(self, S: str) -> str:
STACK = []
TOP = -1
for i in S:
if STACK and STACK[-1] == i:
STACK.pop()
TOP -= 1
else:
STACK.append(i)
TOP += 1
return (''.join(STACK)) | remove-all-adjacent-duplicates-in-string | Python - Stacks (Easy Approach) | piyushagg19 | 1 | 157 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,044 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/336509/Solution-in-Python-3 | class Solution:
def removeDuplicates(self, S: str) -> str:
S, s, t = list(S), 0, 1
if len(set(S)) == 1: return str(S[0]*(len(S)%2))
while s != t:
s, i = len(S), 0
while i < len(S)-1:
if S[i] == S[i+1]:
del S[i:i+2]
i -= 2
i = max(0,i)
i += 1
t = len(S)
return("".join(S))
- Python 3
- Junaid Mansuri | remove-all-adjacent-duplicates-in-string | Solution in Python 3 | junaidmansuri | 1 | 1,000 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,045 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2830730/SIMPLE-PYTHON-SOLUTION | class Solution:
def removeDuplicates(self, s: str) -> str:
st = []
for i in range(0,len(s)):
if len(st)>0 and st[-1]==s[i]:
st.pop()
else:
st.append(s[i])
return ''.join(st) | remove-all-adjacent-duplicates-in-string | SIMPLE PYTHON SOLUTION | ameenusyed09 | 0 | 5 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,046 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2820503/Recursion-with-explanation!-Easy-and-Simple-(Stack-alternative) | class Solution:
def removeDuplicates(self, s: str) -> str:
def fun(string, str_list, index):
if index >= len(string):
return str_list
curr_ele = string[index]
if str_list[-1] != curr_ele:
str_list.append(string[index])
else:
str_list.pop()
return fun(string, str_list, index+1)
return ''.join(fun(s, [' '], 0))[1::] | remove-all-adjacent-duplicates-in-string | Recursion with explanation! Easy and Simple (Stack alternative) | pritul | 0 | 1 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,047 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2806945/Python-Solution | class Solution:
def removeDuplicates(self, s: str) -> str:
def remove(left: int, right: int) -> str:
return s[:left] + s[right:]
while len(s) > 1:
n = len(s)
find = False
i = 0
while i < n - 1:
if s[i] == s[i + 1]:
s = remove(i, i + 2)
find = True
n -= 2
i = max(0, i - 1)
else:
i += 1
if not find:
break
return s | remove-all-adjacent-duplicates-in-string | Python Solution | mansoorafzal | 0 | 4 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,048 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2802557/Use-Stack-easy-approach | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = [s[0]]
temp = s[0]
for i in range(1,len(s)):
if len(stack) == 0:
temp = s[i]
stack.append(s[i])
else:
if s[i] == stack[-1]:
temp = s[i]
stack.pop()
else:
temp = s[i]
stack.append(s[i])
return ''.join([ i for i in stack]) | remove-all-adjacent-duplicates-in-string | Use Stack , easy approach | user8539if | 0 | 5 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,049 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2801710/remove-all-adjacent-duplicates-py3 | class Solution:
def removeDuplicates(self, s: str) -> str:
l=[]
for i in s:
if len(l)>0:
if i !=l[-1]:l.append(i)
else:l.pop(-1)
else:l.append(i)
return ''.join(l) | remove-all-adjacent-duplicates-in-string | remove all adjacent duplicates -py3 | bhargavikanchamreddy | 0 | 3 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,050 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2801315/Like-a-stack | class Solution:
def removeDuplicates(self, s: str) -> str:
removed = False
# Lo gestisco come se fosse uno stack
stack = []
for x in s:
if len(stack) == 0:
stack.append(x)
else:
if stack[len(stack)-1] == x:
stack.pop()
else:
stack.append(x)
# Trasformo tutto in una stringa
ret = ""
while len(stack) > 0:
ret = stack.pop() + ret
return ret | remove-all-adjacent-duplicates-in-string | Like a stack | alessandromariani | 0 | 1 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,051 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2801262/Stack-in-Python | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for letter in s:
if len(stack) == 0:
stack.append(letter)
continue
if stack[-1] == letter:
stack.pop()
continue
stack.append(letter)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Stack in Python | DNST | 0 | 4 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,052 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2801232/Python-or-Stack-or-TC-O(n)-SC-O(n) | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for char in s:
if len(stack) > 0 and stack[-1] == char:
stack.pop()
else:
stack.append(char)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Python | Stack | TC O(n), SC O(n) | baskvava | 0 | 3 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,053 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2801204/Simple-Stack-Solution-(with-recursive-alternative-example) | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for c in s:
if not stack:
stack.append(c)
continue
if stack[-1] == c:
stack.pop(-1)
continue
stack.append(c)
return ''.join(stack)
'''
TOO SLOW! Time Limit Exceeded
'''
'''
def removeDuplicates(self, s: str) -> str:
length = len(s)
if length < 2:
return s
for index in range(1, length):
if s[index] == s[index - 1]:
# remove and recurse
new_s = s[:index-1] + s[index+1:]
return self.removeDuplicates(new_s)
return s
''' | remove-all-adjacent-duplicates-in-string | Simple Stack Solution (with recursive alternative example) | theleastinterestingman | 0 | 2 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,054 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2801059/Remove-all-adjacent-duplicates-in-string-or-Python-solution | class Solution:
def removeDuplicates(self, s: str) -> str:
res = []
for i in range(len(s)):
if len(res) == 0:
res.append(s[i])
elif res[-1] == s[i]:
res.pop()
else:
res.append(s[i])
return "".join(res) | remove-all-adjacent-duplicates-in-string | Remove all adjacent duplicates in string | Python solution | nishanrahman1994 | 0 | 5 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,055 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800955/Python-Simple-O(n)-stack-solution | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for i in s:
if stack and stack[-1] == i:
stack.pop()
else:
stack.append(i)
return ''.join(stack) | remove-all-adjacent-duplicates-in-string | [Python] Simple O(n) stack solution | Mark_computer | 0 | 2 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,056 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800936/Python3-(Comments-for-explanation) | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = [] #initialise a stack
for i in s:
if stack and i == stack[-1]: #check if stack isnt empty (for the first character) and check if last character in the stack is same as current one
stack.pop() #pop the item if match found
else:
stack.append(i) #add the character if no match found
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Python3 (Comments for explanation) | utkarshukla1 | 0 | 3 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,057 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800897/Simple-4-step-explanation-with-arrays. | class Solution:
def removeDuplicates(self, s: str) -> str:
storage = []
for i in range(len(s)):
if storage and s[i]==storage[-1]:
storage.pop()
else:
storage.append(s[i])
return "".join(storage) | remove-all-adjacent-duplicates-in-string | Simple 4 step explanation with arrays. | arvindgongati | 0 | 11 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,058 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800875/November-Daily-Challenge-oror-Python | class Solution:
def removeDuplicates(self, s: str) -> str:
a=[]
top=-1
for i in s:
a.append(i)
top+=1
if top>=1:
if a[top]==a[top-1]:
a.pop()
a.pop()
top-=2
return "".join(a) | remove-all-adjacent-duplicates-in-string | November Daily Challenge || Python | soumya262003 | 0 | 1 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,059 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800869/Python-code-pretty-straight-forward-to-understand | class Solution:
def removeDuplicates(self, s: str) -> str:
#create an empty stack
st = []
for character in s:
if st and character==st[-1]:
st.pop()
else:
st.append(character)
return ''.join(st) | remove-all-adjacent-duplicates-in-string | Python code, pretty straight forward to understand | Cybernoodle | 0 | 2 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,060 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800729/Short-Python3-Solution | class Solution:
def removeDuplicates(self, s: str) -> str:
arr = []
for i in s :
arr.append(i)
while len(arr) >= 2 and arr[-1] == arr[-2] :
arr.pop()
arr.pop()
return ''.join(arr) | remove-all-adjacent-duplicates-in-string | Short Python3 Solution | passionFruitFlower | 0 | 5 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,061 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800659/Python-or-GO-for-Remove-All-Adjacent-Duplicates-In-String | class Solution:
def removeDuplicates(self, s: str) -> str:
chars_stack = []
for item in s:
if chars_stack and chars_stack[-1] == item:
chars_stack.pop()
else:
chars_stack.append(item)
return ''.join(chars_stack) | remove-all-adjacent-duplicates-in-string | Python | GO for Remove All Adjacent Duplicates In String | Bassel_Alf | 0 | 4 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,062 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800659/Python-or-GO-for-Remove-All-Adjacent-Duplicates-In-String | class Solution:
def removeDuplicates(self, s: str) -> str:
return reduce(lambda chars_stack, char:chars_stack[:-1] if chars_stack[-1:] == char else chars_stack + char, s) | remove-all-adjacent-duplicates-in-string | Python | GO for Remove All Adjacent Duplicates In String | Bassel_Alf | 0 | 4 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,063 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800620/Python-Stack-easy-Solution | class Solution:
def removeDuplicates(self, s: str) -> str:
stack=[]
for i in s:
if len(stack)==0:
stack.append(i)
else:
if stack[-1]==i:
stack.pop(-1)
else:
stack.append(i)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Python Stack easy Solution | ankansharma1998 | 0 | 1 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,064 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800590/Easy-Python-Solution | class Solution:
def removeDuplicates(self, s: str) -> str:
if s == "":
return ""
stack = []
for i in range(len(s)):
if len(stack) == 0:
stack.append(s[i])
elif stack[-1] == s[i]:
stack.pop(-1)
else:
stack.append(s[i])
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Easy Python Solution | urmil_kalaria | 0 | 4 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,065 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800579/Brute-Force-Python-Solution | class Solution:
def removeDuplicates(self, s: str) -> str:
if (len(s)<2):
return s
l=[x for x in s]
i=0
while(i<(len(l)-1)):
if l[i]==l[i+1]:
l.pop(i)
l.pop(i)
if i==0:
continue
else:
i-=1
else:
i+=1
return ''.join(l) | remove-all-adjacent-duplicates-in-string | Brute Force Python Solution | CharuArora_ | 0 | 2 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,066 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800547/Easy-Python-or-90-Faster-or-Stack-or-O(n)-Time-or-O(n)-Space | class Solution:
def removeDuplicates(self, s: str) -> str:
ans = []
i = 0
n = len(s)
while i<n:
if ans and ans[-1]==s[i]:
ans.pop()
else:
ans.append(s[i])
i+=1
return ''.join(ans) | remove-all-adjacent-duplicates-in-string | Easy Python | 90% Faster | Stack | O(n) Time | O(n) Space | coolakash10 | 0 | 4 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,067 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800519/Python-%2B-O(n)-time-complexity-solution | class Solution:
def removeDuplicates(self, s: str) -> str:
# create a stack
stack = []
# get length of the string
n = len(s)
# iterate over string and check
# if current string is same as
# previous value, if yes remove the
# last value from list else add the
# current value to list
for i in range(n):
if (stack and stack[-1] == s[i]):
stack.pop()
else:
stack.append(s[i])
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Python + O(n) time complexity solution | ratva0717 | 0 | 4 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,068 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800447/Python-Eazy-Peazy | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for ch in s:
if not stack:
stack.append(ch)
continue
if ch == stack[-1]:
stack.pop()
continue
stack.append(ch)
return ''.join(stack) | remove-all-adjacent-duplicates-in-string | Python Eazy Peazy | kingKabali | 0 | 1 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,069 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800442/FASTEST-AND-EASIEST-oror-BEATS-99-SUBMISSIONS | class Solution:
def removeDuplicates(self, s: str) -> str:
st=[]
for c in s:
if st and c==st[-1]:
st.pop()
else:
st.append(c)
return ''.join(st) | remove-all-adjacent-duplicates-in-string | FASTEST AND EASIEST || BEATS 99% SUBMISSIONS | Pritz10 | 0 | 6 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,070 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800440/Python-Stack | class Solution:
def removeDuplicates(self, s: str) -> str:
r = ["@"]
for i in s:
if i==r[-1]:
r.pop()
else:
r.append(i)
return "".join(r[1:]) | remove-all-adjacent-duplicates-in-string | Python Stack | kanakondasaikiran03 | 0 | 1 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,071 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800338/Python-or-Easy-or-Using-Stack | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for char in s:
if not stack:
stack.append(char)
else:
if stack[-1] == char:
stack.pop()
else:
stack.append(char)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | Python | Easy | Using Stack | chandramohanjha036 | 0 | 1 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,072 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800317/Python3-or-Faster-than-86-or-BruteForce-and-Stack | class Solution:
def removeDuplicates(self, s: str) -> str:
for i in range(1, len(s)):
if s[i - 1] == s[i]:
s = s[:i - 1] + s[i + 1:]
return self.removeDuplicates(s)
return s | remove-all-adjacent-duplicates-in-string | Python3 | Faster than 86% | BruteForce and Stack | prameshbajra | 0 | 2 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,073 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800317/Python3-or-Faster-than-86-or-BruteForce-and-Stack | class Solution:
def removeDuplicates(self, s: str) -> str:
st = [s[0]]
for i in range(1, len(s)):
if(len(st) == 0):
st.append(s[i])
else:
if st[-1] == s[i]:
st.pop()
else:
st.append(s[i])
return ''.join(st) | remove-all-adjacent-duplicates-in-string | Python3 | Faster than 86% | BruteForce and Stack | prameshbajra | 0 | 2 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,074 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800273/Python-or-Stack-Single-Pass-O(n)-Time-O(n-2d)-Space | class Solution:
def removeDuplicates(self, s: str) -> str:
ret = ''
for char in s:
if not ret: ret += char
elif char == ret[-1]: ret = ret[:-1:]
else: ret += char
return ret | remove-all-adjacent-duplicates-in-string | Python | Stack Single-Pass O(n) Time, O(n-2d) Space | jessewalker2010 | 0 | 6 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,075 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800200/Python | class Solution:
def removeDuplicates(self, s: str) -> str:
st = []
for i in s:
if st:
if st[-1] != i:
st.append(i)
else:
st.pop()
else:
st.append(i)
return "".join(st) | remove-all-adjacent-duplicates-in-string | Python | lucken99 | 0 | 2 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,076 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800125/Remove-duplicates-with-stack-python-O(n) | class Solution:
def removeDuplicates(self, s: str) -> str:
# s = list(s)
# i=0
# while i<len(s)-1:
# if s[i] == s[i+1]:
# s.pop(i)
# s.pop(i)
# i = max(0, i-1)
# continue
# i+=1
ss = [s[0]]
for letter in s[1:]:
if ss and ss[-1] == letter:
ss.pop(-1)
continue
ss.append(letter)
return "".join(ss) | remove-all-adjacent-duplicates-in-string | Remove duplicates with stack - python - O(n) | DavidCastillo | 0 | 2 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,077 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800066/Python-Simple-Python-Solution-Using-Stack | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = [s[0]]
for char in s[1:]:
if stack and stack[-1] == char:
stack.pop(-1)
else:
stack.append(char)
return ''.join(stack) | remove-all-adjacent-duplicates-in-string | [ Python ] ✅✅ Simple Python Solution Using Stack🥳✌👍 | ASHOK_KUMAR_MEGHVANSHI | 0 | 6 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,078 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800012/Python-Solution-with-clear-explanation. | class Solution:
def removeDuplicates(self, s: str) -> str:
strStack = list(s[::-1])
unmatchedStack = []
while strStack:
if len(strStack) == 1 or strStack[-1] != strStack[-2]:
unmatchedStack.append(strStack.pop())
else:
strStack.pop()
strStack.pop()
if unmatchedStack:
strStack.append(unmatchedStack.pop())
return "".join(unmatchedStack) | remove-all-adjacent-duplicates-in-string | ✅ Python Solution with clear explanation. ✅ | raghupalash | 0 | 3 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,079 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800012/Python-Solution-with-clear-explanation. | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for char in s:
if len(stack) > 0 and char == stack[-1]:
stack.pop()
else:
stack.append(char)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | ✅ Python Solution with clear explanation. ✅ | raghupalash | 0 | 3 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,080 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2800010/Python-3-or-Stack-solution | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = [s[0]]
for i in range(1, len(s)):
if stack and stack[-1] == s[i]:
stack.pop()
else:
stack.append(s[i])
ans = ""
for ch in stack:
ans += ch
return ans | remove-all-adjacent-duplicates-in-string | Python 3 | Stack solution | sweetkimchi | 0 | 1 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,081 |
https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/discuss/2799983/Python-Easy-to-understand-stack-iterative | class Solution:
def removeDuplicates(self, s: str) -> str:
stack = []
for char in s:
if stack:
# check if last element in stack
# equals current char
if char == stack[-1]:
stack.pop()
continue
else:
stack.append(char)
else:
stack.append(char)
return "".join(stack) | remove-all-adjacent-duplicates-in-string | [Python] Easy to understand stack iterative | dlog | 0 | 4 | remove all adjacent duplicates in string | 1,047 | 0.703 | Easy | 17,082 |
https://leetcode.com/problems/longest-string-chain/discuss/2152864/PYTHON-oror-EXPLAINED-oror | class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
dic = {}
for i in words:
dic[ i ] = 1
for j in range(len(i)):
# creating words by deleting a letter
successor = i[:j] + i[j+1:]
if successor in dic:
dic[ i ] = max (dic[i], 1 + dic[successor])
res = max(dic.values())
return res | longest-string-chain | ✔️ PYTHON || EXPLAINED || ;] | karan_8082 | 27 | 1,500 | longest string chain | 1,048 | 0.591 | Medium | 17,083 |
https://leetcode.com/problems/longest-string-chain/discuss/2152903/Python-DP-2-approaches-using-DP | class Solution:
def longestStrChain(self, words: List[str]) -> int:
n=len(words)
words_set={w:idx for idx, w in enumerate(words)}
@cache
def dp(i):
curr=words[i]
max_length=0
for idx in range(len(curr)):
new_wc = curr[:idx] + curr[idx+1:]
if new_wc in words_set:
max_length=max(max_length, 1 + dp(words_set[new_wc]))
return max_length
return max(dp(i)+1 for i in range(n)) | longest-string-chain | Python DP 2 approaches using DP ✅ | constantine786 | 9 | 492 | longest string chain | 1,048 | 0.591 | Medium | 17,084 |
https://leetcode.com/problems/longest-string-chain/discuss/2152903/Python-DP-2-approaches-using-DP | class Solution:
def longestStrChain(self, words: List[str]) -> int:
w_set = set(words)
@cache
def dp(w:str) -> int:
return 1 + max((dp(s) for i in range(len(w)) if (s:=w[:i]+w[i+1:]) in w_set), default=0)
return max(dp(w) for w in words) | longest-string-chain | Python DP 2 approaches using DP ✅ | constantine786 | 9 | 492 | longest string chain | 1,048 | 0.591 | Medium | 17,085 |
https://leetcode.com/problems/longest-string-chain/discuss/2152903/Python-DP-2-approaches-using-DP | class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
dp={}
for w in words:
dp[w] = max(dp.get(w[:i] + w[i + 1:], 0) + 1 for i in range(len(w)))
return max(dp.values()) | longest-string-chain | Python DP 2 approaches using DP ✅ | constantine786 | 9 | 492 | longest string chain | 1,048 | 0.591 | Medium | 17,086 |
https://leetcode.com/problems/longest-string-chain/discuss/479539/Python3-easy-to-understand-DP-solution | class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
dp = {}
for word in words:
for i in range(len(word)):
if word not in dp:
dp[word]=dp.get(word[:i]+word[i+1:],0)+1
else:
dp[word]=max(dp.get(word[:i]+word[i+1:],0)+1,dp[word])
return max(dp.values()) | longest-string-chain | Python3 easy to understand DP solution | jb07 | 9 | 1,300 | longest string chain | 1,048 | 0.591 | Medium | 17,087 |
https://leetcode.com/problems/longest-string-chain/discuss/868312/Python3-dp-LIS | class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key = lambda x: len(x))
dp = {word: 1 for word in words}
for i in range(len(words)):
word = words[i]
for j in range(len(word)):
tmp = word[: j] + word[j + 1:]
if tmp in dp:
dp[word] = max(dp[tmp] + 1, dp[word])
return max(dp.values()) | longest-string-chain | Python3 dp LIS | ethuoaiesec | 6 | 234 | longest string chain | 1,048 | 0.591 | Medium | 17,088 |
https://leetcode.com/problems/longest-string-chain/discuss/1013746/Python3-dp | class Solution:
def longestStrChain(self, words: List[str]) -> int:
seen = {}
for word in sorted(words, key=len):
seen[word] = 1
for i in range(len(word)):
key = word[:i] + word[i+1:]
if key in seen:
seen[word] = max(seen[word], 1 + seen[key])
return max(seen.values()) | longest-string-chain | [Python3] dp | ye15 | 4 | 284 | longest string chain | 1,048 | 0.591 | Medium | 17,089 |
https://leetcode.com/problems/longest-string-chain/discuss/2420998/Python-or-Dynamic-Programming-or-faster-than-95 | class Solution:
def longestStrChain(self, words: List[str]) -> int:
answer = -1
words_set = set(words)
words.sort(key=lambda x: -len(x))
@cache
def dp(word):
max_p = 1
for i in range(len(word)):
new_w = word[:i] + word[i+1:]
if new_w in words_set:
max_p = max(max_p, 1+dp(new_w))
return max_p
for word in words:
answer = max(answer, dp(word))
return answer | longest-string-chain | Python | Dynamic Programming | faster than 95% | pivovar3al | 3 | 208 | longest string chain | 1,048 | 0.591 | Medium | 17,090 |
https://leetcode.com/problems/longest-string-chain/discuss/2154668/Python-or-Easy-solution-using-hashmap | class Solution:
def longestStrChain(self, words: List[str]) -> int:
words.sort(key=len)
d = {}
for i in words:
d[i] = 1
for j in range(len(i)):
successor = i[:j] + i[j+1:]
if successor in d:
d[i] = max(d[i], 1 + d[successor])
return max(d.values()) | longest-string-chain | Python | Easy solution using hashmap | __Asrar | 2 | 76 | longest string chain | 1,048 | 0.591 | Medium | 17,091 |
https://leetcode.com/problems/longest-string-chain/discuss/2152994/Python-or-DP-or-With-Explanation-or-Easy-to-Understand | class Solution:
def longestStrChain(self, words: List[str]) -> int:
# d[word] is the longest chain ending at word.
# We sort the words by length, iterate through them, and generate all predecessors by removing letters.
# If a predecessor p is in d, d[word] = max(1 + d[p], d[word])
# We can track the max value along the way as well.
# Time Complexity:
# Building the DP dictionary: O(n * k) where k is the length of longest word in words.
# For each word we do len(word) operations to calculate predecessors.
# Lookups and updates to the dict are O(1), so our total time is O(n * k).
# Space Complexity:
# Building the DP dictionary: O(n), since we have 1 entry for each word so O(n) overall.
words.sort(key = lambda x:len(x))
dp = {word:1 for word in words}
answer = 1
for word in words:
for i in range(len(word)):
prev = word[:i] + word[i+1:]
if prev in dp:
dp[word] = max(1+dp[prev], dp[word])
answer = max(answer, dp[word])
return answer | longest-string-chain | Python | DP | With Explanation | Easy to Understand | Mikey98 | 2 | 70 | longest string chain | 1,048 | 0.591 | Medium | 17,092 |
https://leetcode.com/problems/longest-string-chain/discuss/829089/Python-3-or-DP-O(n*n*k)-or-Explanation | class Solution:
def longestStrChain(self, words: List[str]) -> int:
def chain(w1, w2): # check if w1 is a chain to w2
m, n = len(w1), len(w2)
if abs(m-n) != 1: return False
i, j, one = 0, 0, 1
while i < m and j < n:
if w1[i] == w2[j]: i, j = i+1, j+1
elif one: one, i = 0, i+1
else: return False
return True
if not words: return 0
words.sort(key=lambda x: len(x))
n = len(words)
dp = [1] * n
ans = 1
for i in range(n):
for j in range(i): # visited all previous words[j] to check if dp[i] can reach a longer chain
if chain(words[i], words[j]):
dp[i] = max(dp[i], dp[j] + 1)
ans = max(ans, dp[i])
return ans | longest-string-chain | Python 3 | DP O(n*n*k) | Explanation | idontknoooo | 2 | 316 | longest string chain | 1,048 | 0.591 | Medium | 17,093 |
https://leetcode.com/problems/longest-string-chain/discuss/2155543/Python-Easy-DP | class Solution:
def longestStrChain(self, words: List[str]) -> int:
dp = {}
res = 1
for word in sorted(words, key=len):
dp[word] = 1
for i in range(len(word)):
new_word = word[:i] + word[i+1:]
if new_word in dp:
dp[word] = max(dp[word], dp[new_word]+1)
res = max(res, dp[word])
return res | longest-string-chain | Python Easy DP | lokeshsenthilkumar | 1 | 55 | longest string chain | 1,048 | 0.591 | Medium | 17,094 |
https://leetcode.com/problems/longest-string-chain/discuss/2153309/Python3-or-DP-or-easy-to-understand-or-Longest-String-Chain | class Solution:
def longestStrChain(self, words: List[str]) -> int:
arr = [(len(e), e) for e in words]
arr.sort()
d = {e:1 for e in words}
for l, e in arr:
self.check(e, d)
return max(d.values())
def check(self, w, d):
for i in range(len(w)):
s = w[:i] + w[i+1:]
if d.get(s, False):
d[w] = max(d[w], d[s]+1)
return d | longest-string-chain | Python3 | DP | easy to understand | Longest String Chain | H-R-S | 1 | 27 | longest string chain | 1,048 | 0.591 | Medium | 17,095 |
https://leetcode.com/problems/longest-string-chain/discuss/1994057/Python-Easy-Solution-oror-Faster-than-99-oror-Dictionary-oror-O(nLog(n)) | class Solution:
def longestStrChain(self, words: List[str]) -> int:
n = len(words)
words.sort(key = lambda x : len(x)) # Sort the words according to their length
dit = {w:1 for w in words} # Store the longest word chain length till key word
for i in range(1,n):
w = words[i]
for j in range(len(w)): # Max len(w) will be 16
new_w = w[:j]+w[j+1:] # new word after removing j-th character
if new_w in dit and dit[new_w]+1>dit[w]:
dit[w] = dit[new_w]+1
return max(dit.values()) | longest-string-chain | Python Easy Solution || Faster than 99% || Dictionary || O(nLog(n)) | Laxman_Singh_Saini | 1 | 106 | longest string chain | 1,048 | 0.591 | Medium | 17,096 |
https://leetcode.com/problems/longest-string-chain/discuss/1580281/Python-or-Bottom-up-or-DP-solution | class Solution:
def longestStrChain(self, words: List[str]) -> int:
preds = defaultdict(set)
wordSet = set(words)
for word in words:
for i in range(len(word)):
temp = word[:i] + word[i+1:]
if temp in wordSet:
preds[word].add(temp)
words.sort(key=lambda x:len(x))
dp = [1] * len(words)
for i in range(len(words) - 1, 0, -1):
for j in range(i - 1, -1, -1):
if words[j] in preds[words[i]]:
dp[j] = max(dp[j], dp[i] + 1)
return max(dp) | longest-string-chain | Python | Bottom up | DP solution | haotiansun96 | 1 | 232 | longest string chain | 1,048 | 0.591 | Medium | 17,097 |
https://leetcode.com/problems/longest-string-chain/discuss/1546097/python3-top-down-dp-dfs-with-memoization-oror-clean-concise-readable | class Solution:
def longestStrChain(self, words: List[str]) -> int:
from functools import lru_cache
words = set(words) # hashset for quick O(1) lookup
# helper function that computes the lsc for given word (str)
# reduces redundant computation using memoization technique
@lru_cache(maxsize=None)
def lsc_helper(word):
if not word:
return 0
if len(word) == 1:
return 1 if word in words else 0
if word not in words:
return 0
word = list(word)
max_len = 0
for i in range(len(word)):
cand_word = ''.join(word[:i] + word[i+1:])
max_len = max(max_len, lsc_helper(cand_word))
return max_len + 1
max_chain = 0
for word in words:
max_chain = max(max_chain, lsc_helper(word))
return max_chain | longest-string-chain | [python3] top-down dp dfs with memoization || clean, concise, readable | kevintancs | 1 | 318 | longest string chain | 1,048 | 0.591 | Medium | 17,098 |
https://leetcode.com/problems/longest-string-chain/discuss/1471604/Python3-soltn-95-faster | class Solution:
def longestStrChain(self, words: List[str]) -> int:
if not words:
return 0
if len(words) == 1:
return 1
words = sorted(words,key=lambda elem:len(elem))
ref = { word:1 for word in words}
for word in words:
for index in range(len(word)):
newWord = word[:index] + word[index+1:]
if newWord in ref:
ref[word] = max(ref[word],ref[newWord] + 1)
if word not in ref:
ref[word] = 1
ls = sorted(ref.items(),key=lambda elem:elem[1],reverse=True)
return ls[0][1] | longest-string-chain | Python3 soltn 95% faster | aarathorn | 1 | 338 | longest string chain | 1,048 | 0.591 | Medium | 17,099 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.