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/assign-cookies/discuss/2463881/Python-Solution-easy-to-understand | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
if len(s)==0:
return 0
i=0
j=0
c=0
g.sort()
s.sort()
while(i!=len(g) and len(s)!=j):
if g[i]<=s[j]:
c+=1
i+=1
... | assign-cookies | Python Solution - easy to understand | T1n1_B0x1 | 1 | 117 | assign cookies | 455 | 0.505 | Easy | 8,100 |
https://leetcode.com/problems/assign-cookies/discuss/2395370/O(nlogn)-python | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g=sorted(g)
s=sorted(s)
i=0
j=0
c=0
while i<len(g) and j<len(s):
if g[i]<=s[j]:
c+=1
i+=1
j+=1
return c | assign-cookies | O(nlogn) python | sunakshi132 | 1 | 143 | assign cookies | 455 | 0.505 | Easy | 8,101 |
https://leetcode.com/problems/assign-cookies/discuss/1848526/simplet-to-understand-or-faster-than-96.16-soln | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
i = 0
j = 0
countCookie = 0
while j < len(s) and i < len(g):
if s[j] >= g[i]:
countCookie += 1
j += 1
... | assign-cookies | simplet to understand | faster than 96.16 % soln | prankurgupta18 | 1 | 124 | assign cookies | 455 | 0.505 | Easy | 8,102 |
https://leetcode.com/problems/assign-cookies/discuss/1833576/Simple-Python-Solution-oror-50-Faster-oror-Memory-less-than-90 | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
ans=0 ; j=0 ; g.sort() ; s.sort()
for i in range(len(g)):
while j<len(s):
if g[i]<=s[j]: ans+=1 ; s.remove(s[j]) ; break
j+=1
return ans | assign-cookies | Simple Python Solution || 50% Faster || Memory less than 90% | Taha-C | 1 | 125 | assign cookies | 455 | 0.505 | Easy | 8,103 |
https://leetcode.com/problems/assign-cookies/discuss/1596746/Python-3-solution | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
children = len(g)
cookies = len(s)
i = j = 0
while i < children and j < cookies:
if g[i] <= s[j]: # cookie j is big enough for child i
i +=... | assign-cookies | Python 3 solution | dereky4 | 1 | 150 | assign cookies | 455 | 0.505 | Easy | 8,104 |
https://leetcode.com/problems/assign-cookies/discuss/1293271/Easy-Python-Solution(97.59) | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
c=0
j=0
g.sort()
s.sort()
for i in s:
if(j<len(g) and i>=g[j]):
c+=1
j+=1
return c | assign-cookies | Easy Python Solution(97.59%) | Sneh17029 | 1 | 489 | assign cookies | 455 | 0.505 | Easy | 8,105 |
https://leetcode.com/problems/assign-cookies/discuss/1220833/Python3-simple-solution-using-sorting-beats-90-users | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort(reverse=True)
s.sort(reverse=True)
i = 0
j = 0
count = 0
while i < len(g) and j < len(s):
if s[j] >= g[i]:
count += 1
j += 1
... | assign-cookies | Python3 simple solution using sorting beats 90% users | EklavyaJoshi | 1 | 92 | assign cookies | 455 | 0.505 | Easy | 8,106 |
https://leetcode.com/problems/assign-cookies/discuss/2820341/Python-two-pointers-O(n) | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
res, p_g, p_s = 0, 0, 0
g, s = sorted(g), sorted(s)
while p_g < len(g) and p_s < len(s):
if s[p_s] >= g[p_g]:
res += 1
p_g += 1
p_s += 1
return re... | assign-cookies | Python, two pointers, O(n) | Gagampy | 0 | 4 | assign cookies | 455 | 0.505 | Easy | 8,107 |
https://leetcode.com/problems/assign-cookies/discuss/2801276/Mi-solucion | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
k=0
t=0
z=0
st=sorted(s)
gt=sorted(g)
while k<len(s) and t<len(g):
v=st[k] >= gt[z]
z+=v
t+=v
k+=1
return t | assign-cookies | Mi solucion | alex41542 | 0 | 1 | assign cookies | 455 | 0.505 | Easy | 8,108 |
https://leetcode.com/problems/assign-cookies/discuss/2793636/Greedy-Two-Pointer-Python-with-Explanation-in-Code | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
# sort children and cookies by increasing sizes
# start with smallest cookie
# loop greed and satisfaction as you go along
# if we can satisfy this child, increment number satisfied and both poin... | assign-cookies | Greedy Two Pointer Python with Explanation in Code | laichbr | 0 | 3 | assign cookies | 455 | 0.505 | Easy | 8,109 |
https://leetcode.com/problems/assign-cookies/discuss/2706752/PYTHON-W-ITERATION | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
count = 0
g.sort()
s.sort()
i = 0
j = 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
count += 1
i += 1
j += 1
... | assign-cookies | PYTHON W/ ITERATION | frankbidak | 0 | 2 | assign cookies | 455 | 0.505 | Easy | 8,110 |
https://leetcode.com/problems/assign-cookies/discuss/2406064/Python-92.62-faster-or-Simplest-solution-with-explanation-or-Beg-to-Adv-or-Greedy | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort() # soring list g
s.sort() # sorting list s
# because it`ll make easy for us to figure out how many children are getting the cookies as per greedy.
childi = 0 # taking variable
c... | assign-cookies | Python 92.62% faster | Simplest solution with explanation | Beg to Adv | Greedy | rlakshay14 | 0 | 150 | assign cookies | 455 | 0.505 | Easy | 8,111 |
https://leetcode.com/problems/assign-cookies/discuss/2212271/Memory-Usage-Less-than-92.18 | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
output = 0
g.sort()
s.sort()
for i in s:
for j in g:
if i>=j:
output+=1
g.remove(j)
break
return output | assign-cookies | Memory Usage Less than 92.18% | jayeshvarma | 0 | 47 | assign cookies | 455 | 0.505 | Easy | 8,112 |
https://leetcode.com/problems/assign-cookies/discuss/1587470/Python-solution-using-sorting-and-2-pointers. | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
ch=0
co=0
count=0
g=sorted(g)
s=sorted(s)
while ch<len(g) and co<len(s):
if g[ch]<=s[co]:
count+=1
ch+=1
co+=1
els... | assign-cookies | Python solution using sorting and 2 pointers. | prajwalahluwalia | 0 | 54 | assign cookies | 455 | 0.505 | Easy | 8,113 |
https://leetcode.com/problems/assign-cookies/discuss/1308066/Python-Solution-with-Sorting | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
if not s:
return 0
g.sort(reverse=True)
s.sort(reverse=True)
greed, cookie = 0,0
ret = 0
while greed < len(g) and cookie < len(s):
if g[greed] <... | assign-cookies | Python Solution with Sorting | 5tigerjelly | 0 | 114 | assign cookies | 455 | 0.505 | Easy | 8,114 |
https://leetcode.com/problems/assign-cookies/discuss/1278808/python3-dollarolution(98-faster) | class Solution:
from collections import Counter
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g = sorted(g)
s = sorted(s)
c, j = 0, 1
for i in g[::-1]:
if j <= len(s):
if i <= s[-j]:
c += 1
j ... | assign-cookies | python3 $olution(98% faster) | AakRay | 0 | 155 | assign cookies | 455 | 0.505 | Easy | 8,115 |
https://leetcode.com/problems/assign-cookies/discuss/1163408/Python3-O(nlogn)-time-O(1)-space | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
response = 0
while g and s:
if s[-1] >= g[-1]:
s.pop()
g.pop()
response += 1
else:
g.pop()
... | assign-cookies | Python3 O(nlogn) time O(1) space | peterhwang | 0 | 193 | assign cookies | 455 | 0.505 | Easy | 8,116 |
https://leetcode.com/problems/assign-cookies/discuss/1007124/Easy-and-simple-solution | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
i,j,x=0,0,0
g.sort()
s.sort()
while i<len(g) and j<len(s):
if s[j]>=g[i]:
j+=1
i+=1
x+=1
else:
j+=1
retur... | assign-cookies | Easy and simple solution | thisisakshat | 0 | 138 | assign cookies | 455 | 0.505 | Easy | 8,117 |
https://leetcode.com/problems/assign-cookies/discuss/795367/Python3-O(nlogn)-two-pointers-beats-99 | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g,s = sorted(g), sorted(s)
lg, ls = len(g), len(s)
sp = ls-1
ans = 0
for i in range(lg-1, -1, -1):
if sp < 0:
break
... | assign-cookies | Python3, O(nlogn), two pointers, beats 99% | amey619rocks | 0 | 74 | assign cookies | 455 | 0.505 | Easy | 8,118 |
https://leetcode.com/problems/assign-cookies/discuss/785679/Python-O(n-log-n) | class Solution:
def findContentChildren(self, children: [int], cookies: [int]) -> int:
children.sort()
cookies.sort()
counter = 0
for cookie in cookies:
if counter == len(children):
break
if cookie >= children[counter]: # Is the cookie ok for t... | assign-cookies | Python O(n log n) | devMEremenko | 0 | 114 | assign cookies | 455 | 0.505 | Easy | 8,119 |
https://leetcode.com/problems/assign-cookies/discuss/1352146/Simple-Python-Greedy-Approach | class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
i = j = 0
out = 0
while i < len(g) and j < len(s):
if g[i] <= s[j]:
i += 1
out += 1
j += 1
return out | assign-cookies | Simple Python Greedy Approach | Kenzjk | -2 | 213 | assign cookies | 455 | 0.505 | Easy | 8,120 |
https://leetcode.com/problems/132-pattern/discuss/2015125/Python-Solution-using-Stack | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
if len(nums)<3:
return False
second_num = -math.inf
stck = []
# Try to find nums[i] < second_num < stck[-1]
for i in range(len(nums) - 1, -1, -1):
if nums[i] < second_num:
... | 132-pattern | ✅ Python Solution using Stack | constantine786 | 37 | 3,400 | 132 pattern | 456 | 0.325 | Medium | 8,121 |
https://leetcode.com/problems/132-pattern/discuss/2387128/Very-Easy-100-(Fully-Explained)-(Java-C%2B%2B-Python-JS-C-Python3) | class Solution(object):
def find132pattern(self, nums):
# Base Condition...
if len(nums) < 3:
return False
m = float('-inf')
# Initialise a empty stack...
stack = []
# Run a Loop from last to first index...
for i in range(len(nums)-1, -1, -1):
... | 132-pattern | Very Easy 100% (Fully Explained) (Java, C++, Python, JS, C, Python3) | PratikSen07 | 5 | 466 | 132 pattern | 456 | 0.325 | Medium | 8,122 |
https://leetcode.com/problems/132-pattern/discuss/2387128/Very-Easy-100-(Fully-Explained)-(Java-C%2B%2B-Python-JS-C-Python3) | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
# Base Condition...
if len(nums) < 3:
return False
# To keep track of minimum element...
mini = float('-inf')
# Initialise a empty stack...
stack = []
# Run a Loop from last to firs... | 132-pattern | Very Easy 100% (Fully Explained) (Java, C++, Python, JS, C, Python3) | PratikSen07 | 5 | 466 | 132 pattern | 456 | 0.325 | Medium | 8,123 |
https://leetcode.com/problems/132-pattern/discuss/1296419/easy-understanding-solution-with-comments-or-python-or-stack | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
mi = [nums[0]]
n=len(nums)
# making a min stack which store the minimum element till the current index from left
for j in range(1,n):
mi.append( min(mi[-1],nums[j]) )
... | 132-pattern | easy understanding solution with comments | python | stack | chikushen99 | 5 | 588 | 132 pattern | 456 | 0.325 | Medium | 8,124 |
https://leetcode.com/problems/132-pattern/discuss/1640069/Python3-Solution-or-100-faster | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
stack = []
s2 = float('-inf')
for i in nums[::-1]:
if i<s2: return True
while stack and i>stack[-1]:
s2 = stack.pop()
stack.append(i)
return False | 132-pattern | Python3 Solution | 100% faster | satyam2001 | 4 | 663 | 132 pattern | 456 | 0.325 | Medium | 8,125 |
https://leetcode.com/problems/132-pattern/discuss/907508/Python-Solution-Explained-(video-%2B-code) | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
# i , j, k
# i -> get val from min_list
# j -> iterate through nums for each j val : nums[indx]
# k -> get vals using stack
min_list = []
stack = []
# Building Min list
min_lis... | 132-pattern | Python Solution Explained (video + code) | spec_he123 | 3 | 401 | 132 pattern | 456 | 0.325 | Medium | 8,126 |
https://leetcode.com/problems/132-pattern/discuss/2016218/Easiest-Python-Solution-with-stack | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
""" Monotonic decreasing stack """
st=[]
""" Assume first element as minimum"""
minn=nums[0]
for i in nums[1:]:
""" We try to maintain the highest value at the top of the stacksuch that it is greater than i too ."""
... | 132-pattern | Easiest Python Solution with stack | a_dityamishra | 2 | 180 | 132 pattern | 456 | 0.325 | Medium | 8,127 |
https://leetcode.com/problems/132-pattern/discuss/2016218/Easiest-Python-Solution-with-stack | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
st=[]
minn=nums[0]
for i in nums[1:]:
while st and i>=st[-1][0]:
st.pop()
if st and i>st[-1][1]:
return True
st.append([i,minn])
minn=min(minn,i)... | 132-pattern | Easiest Python Solution with stack | a_dityamishra | 2 | 180 | 132 pattern | 456 | 0.325 | Medium | 8,128 |
https://leetcode.com/problems/132-pattern/discuss/2015601/Python-oror-Monotonic-Stack-O(N) | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
stack = []
minVal = nums[0]
for i in range(1,len(nums)):
# stack should be monotonic decreasing
while stack and nums[i]>=stack[-1][0]:
stack.pop()
if s... | 132-pattern | Python || Monotonic Stack - O(N) | gamitejpratapsingh998 | 2 | 319 | 132 pattern | 456 | 0.325 | Medium | 8,129 |
https://leetcode.com/problems/132-pattern/discuss/2023693/Python-Solution | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
stack = []
pattern_min = nums[0]
for i in nums[1:]:
while stack and i >= stack[-1][0]:
stack.pop()
if stack and i > stack[-1][1]:
return True
stack.append([i... | 132-pattern | 🔴 Python Solution 🔴 | alekskram | 1 | 152 | 132 pattern | 456 | 0.325 | Medium | 8,130 |
https://leetcode.com/problems/132-pattern/discuss/848748/Python3-two-approaches | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
stack = [] # mono stack (decreasing)
mn = [] # minimum so far
for i, x in enumerate(nums):
mn.append(min(mn[-1], x) if mn else x)
while stack and stack[-1][1] <= x: stack.pop() # find latest element ... | 132-pattern | [Python3] two approaches | ye15 | 1 | 274 | 132 pattern | 456 | 0.325 | Medium | 8,131 |
https://leetcode.com/problems/132-pattern/discuss/848748/Python3-two-approaches | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
stack = [] # mono stack (decreasing)
ref = -inf
for x in reversed(nums): # reversed 2-3-1 pattern
if x < ref: return True
while stack and stack[-1] < x: ref = stack.pop()
stack.append(x)
... | 132-pattern | [Python3] two approaches | ye15 | 1 | 274 | 132 pattern | 456 | 0.325 | Medium | 8,132 |
https://leetcode.com/problems/132-pattern/discuss/488496/Python3-stack-solution | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
if len(nums)<3: return False
temp,n,mins = [],len(nums),[0]*len(nums)
mins[0] = nums[0]
for i in range(1,n):
mins[i] = min(mins[i-1],nums[i])
for i in range(n-1,-1,-1):
if nums[i] > min... | 132-pattern | Python3 stack solution | jb07 | 1 | 312 | 132 pattern | 456 | 0.325 | Medium | 8,133 |
https://leetcode.com/problems/132-pattern/discuss/2535001/Python-Solution-or-3-methods-or-Brute-Force-or-BS-or-Monotonic-Stack | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
n=len(nums)
# Brute Force: O(n^3) --> TLE
# for i in range(n):
# for j in range(i+1, n):
# for k in range(j+1, n):
# if nums[i]<nums[k] and nums[k]<nums[j]:
# ... | 132-pattern | Python Solution | 3 methods | Brute Force | BS | Monotonic Stack | Siddharth_singh | 0 | 90 | 132 pattern | 456 | 0.325 | Medium | 8,134 |
https://leetcode.com/problems/132-pattern/discuss/2016648/10-Lines-of-python-Code-with-Video-Explanation | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
stack = [] # [nums,minleft]
minCurr = nums[0]
for n in nums[1:]:
while stack and stack[-1][0] <= n:
stack.pop()
if stack and stack[-1][1] < n:
r... | 132-pattern | 10 Lines of python Code with Video Explanation | prernaarora221 | 0 | 97 | 132 pattern | 456 | 0.325 | Medium | 8,135 |
https://leetcode.com/problems/132-pattern/discuss/2016606/Python3-Solution-with-using-stack | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
stack = []
last_pattern_element = float('-inf')# like 2 from 132
for i in range(len(nums) - 1, -1, -1):
if nums[i] < last_pattern_element:
return True
while stack ... | 132-pattern | [Python3] Solution with using stack | maosipov11 | 0 | 22 | 132 pattern | 456 | 0.325 | Medium | 8,136 |
https://leetcode.com/problems/132-pattern/discuss/2015959/Python-or-TC-O(N)SC-O(N)-or-Using-Stack-or-Easy-to-understand-solution | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
# stack will contain = [top,minLeft]
stack = []
currMin = nums[0]
# try to compare minLeftToStack < curr < stackTop
for curr in nums[1:]:
#make sure your stack is strictly increa... | 132-pattern | Python | TC-O(N)/SC-O(N) | Using Stack | Easy to understand solution | Patil_Pratik | 0 | 29 | 132 pattern | 456 | 0.325 | Medium | 8,137 |
https://leetcode.com/problems/132-pattern/discuss/2015746/Easy-Python-Solution-with-comments | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
#required: num1 < num3< num2
if len(nums) < 3:
return False
#Initialize the num_3 to a min number and find a number
#such that num_3 < num_2
num_3, stack = -10**9, []
for i in range(len(nu... | 132-pattern | Easy Python Solution with comments | firefist07 | 0 | 92 | 132 pattern | 456 | 0.325 | Medium | 8,138 |
https://leetcode.com/problems/132-pattern/discuss/2015566/python-stack-solution-(Time-On-space-O1) | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
if(len(nums) < 3): return False
mini = [nums[0]]
for i in range (1, len(nums)):
mini.append(min(mini[-1], nums[i]))
maxi = []
for i in range (len(nums)-1, -1, -1):
if mini[i] < nums[i]:
while len(... | 132-pattern | python - stack solution (Time On, space O1) | ZX007java | 0 | 51 | 132 pattern | 456 | 0.325 | Medium | 8,139 |
https://leetcode.com/problems/132-pattern/discuss/650438/Python3-O(n)-solution-using-previous-greater-element-132-Pattern | class Solution:
def find132pattern(self, nums: List[int]) -> bool:
prev_greater = [-1] * len(nums)
stack = []
for i, n in enumerate(nums):
# Use >= so that PGE is strictly greater as opposed to greater or equal
while stack and n >= nums[stack[-1]]:
sta... | 132-pattern | Python3 O(n) solution using previous greater element - 132 Pattern | r0bertz | 0 | 590 | 132 pattern | 456 | 0.325 | Medium | 8,140 |
https://leetcode.com/problems/circular-array-loop/discuss/1317119/Python-3-or-Short-Python-Set-or-Explanation | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
n, visited = len(nums), set()
for i in range(n):
if i not in visited:
local_s = set()
while True:
if i in local_s: return True
if i in visited: br... | circular-array-loop | Python 3 | Short Python, Set | Explanation | idontknoooo | 10 | 1,200 | circular array loop | 457 | 0.323 | Medium | 8,141 |
https://leetcode.com/problems/circular-array-loop/discuss/1873232/Self-Understandable-Python-(explained-code-%2B-98-faster)-%3A | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
for i in range(len(nums)):
seen=set()
while True:
if i in seen: # if index already exist in set means, array is circular
return True
seen.add(i)
... | circular-array-loop | Self Understandable Python (explained code + 98% faster) : | goxy_coder | 3 | 263 | circular array loop | 457 | 0.323 | Medium | 8,142 |
https://leetcode.com/problems/circular-array-loop/discuss/1873232/Self-Understandable-Python-(explained-code-%2B-98-faster)-%3A | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
seen=set()
for i in range(len(nums)):
if i not in seen:
local=set()
while True:
if i in local:
return True
if i in seen:
... | circular-array-loop | Self Understandable Python (explained code + 98% faster) : | goxy_coder | 3 | 263 | circular array loop | 457 | 0.323 | Medium | 8,143 |
https://leetcode.com/problems/circular-array-loop/discuss/1092712/Python-or-two-pointers-or-O(N2)-Time-or-O(1)-Space | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
def get_next_index(nums, cur_index, is_positive):
direction = nums[cur_index] >= 0
if direction != is_positive:
return -1
next_index = (cur_index+nums[cur_index])%len(nums)
... | circular-array-loop | Python | two-pointers | O(N^2) Time | O(1) Space | Rakesh301 | 2 | 371 | circular array loop | 457 | 0.323 | Medium | 8,144 |
https://leetcode.com/problems/circular-array-loop/discuss/1686706/Python3-Use-different-markers-for-each-traversal | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
"""So we have been having trouble finding a decent way to tell the size
of the loop and reject a loop if its size is 1. This solution
Ref: https://leetcode.com/problems/circular-array-loop/discuss/232417/Python-simple-sol... | circular-array-loop | [Python3] Use different markers for each traversal | FanchenBao | 1 | 99 | circular array loop | 457 | 0.323 | Medium | 8,145 |
https://leetcode.com/problems/circular-array-loop/discuss/1190478/24ms-98-efficient. | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
"""
Bruteforce Solution with some improvement
"""
size = len(nums)
visited = [False]*size
v = 0
cycle_idx = 0
i = 0
#check cycle from all index
... | circular-array-loop | 24ms , 98% efficient. | vikash4466kumar | 1 | 422 | circular array loop | 457 | 0.323 | Medium | 8,146 |
https://leetcode.com/problems/circular-array-loop/discuss/2690453/python | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
n = len(nums)
def next(cur):
return (cur + nums[cur]) % n
for i, num in enumerate(nums):
if num == 0:
continue
slow = i
fast = next(i)
while nums[... | circular-array-loop | python | xy01 | 0 | 15 | circular array loop | 457 | 0.323 | Medium | 8,147 |
https://leetcode.com/problems/circular-array-loop/discuss/2282658/Python-using-two-sets.-Time%3A-O(N)-Space%3A-O(N) | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
def isCycle(i, num):
seen = set()
while i not in seen:
seen.add(i)
ni = (i + nums[i]) % length
if ni in checked or i == ni or nums[ni] * num <= 0:
... | circular-array-loop | Python, using two sets. Time: O(N), Space: O(N) | blue_sky5 | 0 | 94 | circular array loop | 457 | 0.323 | Medium | 8,148 |
https://leetcode.com/problems/circular-array-loop/discuss/2219601/Slow-fast-pointers-with-Set-or-Python-or-O(n)-time-O(n)-space | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
def find_next_index(index, direction):
new_dir = nums[index] > 0
if new_dir != direction: # accept only one direction
return -1
new_index = (index + nums[index]) % n
... | circular-array-loop | Slow, fast pointers with Set | Python | O(n) time, O(n) space | tramnhatquang | 0 | 121 | circular array loop | 457 | 0.323 | Medium | 8,149 |
https://leetcode.com/problems/circular-array-loop/discuss/849857/Python3-two-memo-tables | class Solution:
def circularArrayLoop(self, nums: List[int]) -> bool:
seen = set() # visited & no cycle
for i, x in enumerate(nums):
if i in seen: continue
temp = set() # visited in this round
while True:
ii = (i + nums[i])% len(nums)
... | circular-array-loop | [Python3] two memo tables | ye15 | 0 | 144 | circular array loop | 457 | 0.323 | Medium | 8,150 |
https://leetcode.com/problems/poor-pigs/discuss/935581/C%2B%2BPythonPicture-1-line-greedy-solution-with-N-dimension-puzzle-cube-scan | class Solution:
def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
return ceil(log(buckets) / log(minutesToTest / minutesToDie + 1)); | poor-pigs | [C++/Python/Picture] 1-line greedy solution with N-dimension puzzle cube scan | codedayday | 34 | 3,400 | poor pigs | 458 | 0.643 | Hard | 8,151 |
https://leetcode.com/problems/poor-pigs/discuss/2387610/Python-oror-Detailed-Explanation-oror-Faster-Than-98-oror-Easily-Understood-oror-Simple-oror-MATH | class Solution:
def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
return math.ceil(math.log(buckets, minutesToTest/minutesToDie + 1)) | poor-pigs | 🔥 Python || Detailed Explanation ✅ || Faster Than 98% || Easily Understood || Simple || MATH | wingskh | 26 | 461 | poor pigs | 458 | 0.643 | Hard | 8,152 |
https://leetcode.com/problems/poor-pigs/discuss/2398430/Easy-0-ms-100-(Fully-Explained)(Java-C%2B%2B-Python-JS-C-Python3) | class Solution(object):
def poorPigs(self, buckets, minutesToDie, minutesToTest):
# Calculate the max time for a pig to test buckets...
# Note that, max time will not be (minutesToTest / minutesToDie)...
# Thinking about all pigs drinking all buckets at last, but no one died immediately, so ... | poor-pigs | Easy 0 ms 100% (Fully Explained)(Java, C++, Python, JS, C, Python3) | PratikSen07 | 7 | 403 | poor pigs | 458 | 0.643 | Hard | 8,153 |
https://leetcode.com/problems/poor-pigs/discuss/2398430/Easy-0-ms-100-(Fully-Explained)(Java-C%2B%2B-Python-JS-C-Python3) | class Solution:
def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int:
# Calculate the max time for a pig to test buckets...
# Note that, max time will not be (minutesToTest / minutesToDie)...
# Thinking about all pigs drinking all buckets at last, but no one died im... | poor-pigs | Easy 0 ms 100% (Fully Explained)(Java, C++, Python, JS, C, Python3) | PratikSen07 | 7 | 403 | poor pigs | 458 | 0.643 | Hard | 8,154 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2304034/Python-93.74-fasters-or-Python-Simplest-Solution-With-Explanation-or-Beg-to-adv-or-Slicing | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
return s in s[1:] + s[:-1] | repeated-substring-pattern | Python 93.74% fasters | Python Simplest Solution With Explanation | Beg to adv | Slicing | rlakshay14 | 6 | 332 | repeated substring pattern | 459 | 0.437 | Easy | 8,155 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2818673/Python-oror-97.68-Faster-oror-Two-Solutionsoror-One-Liner | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
# Here we checking that s is present in a new string double of s which after remvoing fast and last element
return s in s[1:] + s[:-1] | repeated-substring-pattern | Python || 97.68% Faster || Two Solutions|| One Liner | DareDevil_007 | 4 | 144 | repeated substring pattern | 459 | 0.437 | Easy | 8,156 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2818673/Python-oror-97.68-Faster-oror-Two-Solutionsoror-One-Liner | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
n,t=len(s),''
for i in range(n//2):
t+=s[i]
if t*(n//(i+1))==s: return True
return False | repeated-substring-pattern | Python || 97.68% Faster || Two Solutions|| One Liner | DareDevil_007 | 4 | 144 | repeated substring pattern | 459 | 0.437 | Easy | 8,157 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2250534/PYTHON-or-ONE-LINER-orFASTER-THAN-97or-EASY | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
return s in s[1:]+s[:-1] | repeated-substring-pattern | PYTHON | ONE-LINER |FASTER THAN 97%| EASY | vatsalg2002 | 4 | 240 | repeated substring pattern | 459 | 0.437 | Easy | 8,158 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/1774418/Python-3-easy-solution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
n = len(s)
sub = ''
for i in range(n // 2):
sub += s[i]
k, r = divmod(n, i + 1)
if r == 0 and sub * k == s:
return True
return False | repeated-substring-pattern | Python 3 easy solution | dereky4 | 3 | 809 | repeated substring pattern | 459 | 0.437 | Easy | 8,159 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/1551412/Simple-Python-solution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
ss = ""
for i in s:
ss += i
times = len(s)//len(ss)
if times==1:
break
if (ss*times)==s:
return True
return False | repeated-substring-pattern | Simple Python solution | Pritish0173 | 2 | 245 | repeated substring pattern | 459 | 0.437 | Easy | 8,160 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/1067224/Python-or-AC-Solution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
for i in range(1, (len(s)//2)+1):
if len(s) % i != 0: continue
pattern = s[0:i]
if pattern*(len(s)//i) == s:
return True
return False | repeated-substring-pattern | Python | AC Solution | dev-josh | 1 | 282 | repeated substring pattern | 459 | 0.437 | Easy | 8,161 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2834534/Pythonorrolling-hash-solution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
base=123
h=0
n=len(s)
total=[-1]*n
ha=[-1]*n
for i in range(n):
if i==0:
ha[0]=1
else:
ha[i]=ha[i-1]*base
ha[i]%=(10**9+7)
... | repeated-substring-pattern | Python|rolling hash solution | wxzhang3 | 0 | 3 | repeated substring pattern | 459 | 0.437 | Easy | 8,162 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2821810/Python-46ms-13.8-MB-(beats-92.12-in-time-and-90.45-in-mem) | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
length = len(s)
for i in range(1, length // 2 + 1):
if length % i:
continue
if s[:i] * (length // i) == s:
return True
return False | repeated-substring-pattern | Python 46ms 13.8 MB (beats 92.12% in time and 90.45 % in mem) | user3687fV | 0 | 4 | repeated substring pattern | 459 | 0.437 | Easy | 8,163 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2803503/KMP-ALGORITHM-CONCEPT-or-O(N)-or-PYTHON3-or-OPTIMAL-SOLUTION | class Solution:
def computeLPS(self, s , n , lps):
i = 0 # for matching characters
j = 1 # move forward for giving lps value for each character
while j < n:
if s[i] == s[j]:
lps[j] = i + 1
i+=1
j+=1
elif i > 0: ... | repeated-substring-pattern | KMP ALGORITHM CONCEPT | O(N) | PYTHON3 | OPTIMAL SOLUTION | vishal7085 | 0 | 7 | repeated substring pattern | 459 | 0.437 | Easy | 8,164 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2779033/python-kmp | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
def gen_next(s):
nex = [0, 0]
j = 0
for i in range(1, len(s)):
while j > 0 and s[j] != s[i]:
j = nex[j]
if s[i] == s[j]:
j += 1
... | repeated-substring-pattern | python kmp | xsdnmg | 0 | 5 | repeated substring pattern | 459 | 0.437 | Easy | 8,165 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2775554/Python-Calculate-length-solution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
length = len(s)
for i, c in enumerate(s):
subLength = i + 1
if length % subLength != 0:
continue
num = length // subLength
if num == 1:
break
... | repeated-substring-pattern | Python Calculate length solution | StacyAceIt | 0 | 5 | repeated substring pattern | 459 | 0.437 | Easy | 8,166 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2726737/Python-solution-with-recursion | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
substring = s[0]
def check_substring(string, substring):
n = len(substring)
if not string: return True
if len(string) < n:
return False
if substring == string[:n]:
... | repeated-substring-pattern | Python solution with recursion | michaelniki | 0 | 4 | repeated substring pattern | 459 | 0.437 | Easy | 8,167 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2714522/PYTHON3-BEST | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
a = "".join( (s[1:], s[:-1]) )
return s in a | repeated-substring-pattern | PYTHON3 BEST | Gurugubelli_Anil | 0 | 7 | repeated substring pattern | 459 | 0.437 | Easy | 8,168 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2651456/Python-Solution-or-One-Liner-or-Pattern-Matching | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
return (s*2)[1:-1].find(s) != -1 | repeated-substring-pattern | Python Solution | One Liner | Pattern Matching | Gautam_ProMax | 0 | 88 | repeated substring pattern | 459 | 0.437 | Easy | 8,169 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2380693/Simple-logic | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
i=0
ans=""
l=len(s)
while i <l:
ans+=s[i]
if l%(i+1)==0:
if ans*(l//(i+1)) == s:
if i==l-1:
return False
r... | repeated-substring-pattern | Simple logic | sunakshi132 | 0 | 152 | repeated substring pattern | 459 | 0.437 | Easy | 8,170 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2204450/Next-array-method-in-KMP-by-Python-easy-to-understand! | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
# Make the next array (the next array in KMP)
next=[-1]*len(s)
j=-1
for i in range(1,len(s)):
while j>-1 and s[j+1]!=s[i]:
j=next[j]
if s[j+1]==s[i]:
j+=1
... | repeated-substring-pattern | Next array method in KMP by Python, easy to understand! | XRFXRF | 0 | 63 | repeated substring pattern | 459 | 0.437 | Easy | 8,171 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2041044/Repeated-Substring-Pattern-(2-lines) | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
s_rol = "".join((s[1:], s[:-1]))
return s in s_rol | repeated-substring-pattern | Repeated Substring Pattern (2 lines) | vaibhav024 | 0 | 208 | repeated substring pattern | 459 | 0.437 | Easy | 8,172 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2019640/Python-two-differnt-approaches | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
return s in (s+s)[1: -1] | repeated-substring-pattern | Python two differnt approaches | theReal007 | 0 | 136 | repeated substring pattern | 459 | 0.437 | Easy | 8,173 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/2019640/Python-two-differnt-approaches | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
if len(s) == 1 :
return False
n = len(s)
string = ''
for i in range(len(s)):
string += s[i]
l = len(string)
rest = n - l
if l > rest : return False
... | repeated-substring-pattern | Python two differnt approaches | theReal007 | 0 | 136 | repeated substring pattern | 459 | 0.437 | Easy | 8,174 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/1946552/Curious-python-solution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
from math import ceil # for odd len strings
sub_str = ''
if len(s) < 2: return False # 2 symb is minimum len
for i in range(ceil(len(s)/2)):
sub_str += s[i]
if sub_str * ceil(len(s)/(i+1)) == ... | repeated-substring-pattern | Curious python solution | StikS32 | 0 | 142 | repeated substring pattern | 459 | 0.437 | Easy | 8,175 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/1753646/Short-easy-to-understand-solution-in-python | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
n = len(s)
if n<2 : return False
for i in range(2,n+1):
if n%i == 0 :
if s == "".join(s[0:n//i]*i) : return True
return False | repeated-substring-pattern | Short easy to understand solution in python | RuettinE | 0 | 136 | repeated substring pattern | 459 | 0.437 | Easy | 8,176 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/976353/Python-Easy-to-Understand | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
inter = ''
for i in range(len(s) // 2):
inter += s[i]
if inter * (len(s) // len(inter)) == s:
return True
return False | repeated-substring-pattern | Python Easy to Understand | Aditya380 | 0 | 152 | repeated substring pattern | 459 | 0.437 | Easy | 8,177 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/826896/Python3-summarizing-a-few-approaches | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
n = len(s)
for k in range(1, n//2 + 1):
if n%k == 0 and s[:k]*(n//k) == s and n//k > 1: return True
return False | repeated-substring-pattern | [Python3] summarizing a few approaches | ye15 | 0 | 29 | repeated substring pattern | 459 | 0.437 | Easy | 8,178 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/826896/Python3-summarizing-a-few-approaches | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
return s in (s*2)[1:-1] | repeated-substring-pattern | [Python3] summarizing a few approaches | ye15 | 0 | 29 | repeated substring pattern | 459 | 0.437 | Easy | 8,179 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/826896/Python3-summarizing-a-few-approaches | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
dp = [0]*len(s)
i, j = 1, 0
while i < len(s):
if s[i] == s[j]:
dp[i] = j+1
i += 1
j += 1
elif j == 0: i += 1
else: j = dp[j-1]
ret... | repeated-substring-pattern | [Python3] summarizing a few approaches | ye15 | 0 | 29 | repeated substring pattern | 459 | 0.437 | Easy | 8,180 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/826508/very-simple-python-solution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
n = len(s)
if n == 1: return False
for i in range(1, n):
if n % i == 0:
if s[ : i] * (n // i) == s: return True
return False | repeated-substring-pattern | very simple python solution | _YASH_ | 0 | 20 | repeated substring pattern | 459 | 0.437 | Easy | 8,181 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/1250439/Python3-simple-solution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
x = s[0]
n = len(x)
m = len(s)
i = 1
while n < m//2+1 and i < len(s)//2+1:
n = len(x)
z = m//n
if x * z == s:
return Tru... | repeated-substring-pattern | Python3 simple solution | EklavyaJoshi | -1 | 146 | repeated substring pattern | 459 | 0.437 | Easy | 8,182 |
https://leetcode.com/problems/repeated-substring-pattern/discuss/1278863/Python3-dollarolution | class Solution:
def repeatedSubstringPattern(self, s: str) -> bool:
a = s[0]
x = len(s)
for i in range(1,len(s)):
if a == s[i:i+len(a)]:
c = int(len(s)/len(a))
if a * c == s:
return True
else:
pass
... | repeated-substring-pattern | Python3 $olution | AakRay | -2 | 681 | repeated substring pattern | 459 | 0.437 | Easy | 8,183 |
https://leetcode.com/problems/hamming-distance/discuss/1585601/Handmade-binary-function-(time%3A-O(L)) | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
def get_bin(num):
res = []
while num > 0:
res.append(num % 2)
num //= 2
return ''.join(str(num) for num in res[::-1])
if x < y:
x, y = y, x
... | hamming-distance | Handmade binary function (time: O(L)) | kryuki | 3 | 121 | hamming distance | 461 | 0.749 | Easy | 8,184 |
https://leetcode.com/problems/hamming-distance/discuss/1099438/Python-one-liner-easy-solution-and-self-explanatory | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return bin(x^y).replace("0b","").count('1') | hamming-distance | Python one liner easy solution and self explanatory | coderash1998 | 2 | 113 | hamming distance | 461 | 0.749 | Easy | 8,185 |
https://leetcode.com/problems/hamming-distance/discuss/2821757/Easy-approach-with-bit-manipulation-with-explanation | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
diff = x^y
res = 0
while diff:
res+= diff&1
diff>>=1
return res | hamming-distance | Easy approach with bit manipulation with explanation | mazurkaterina | 1 | 7 | hamming distance | 461 | 0.749 | Easy | 8,186 |
https://leetcode.com/problems/hamming-distance/discuss/2688279/Python3-oror-Two-ways-string-and-bit-manipulation-(Commented) | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
# String solution
# TC O(n) SC O(1)
longer = len(bin(y))-2
if x > y: longer = len(bin(x))-2
# 2 lines above for padding 0's
# turn both x and y to binary, keep count of mismatches with count variabl... | hamming-distance | Python3 || Two ways, string and bit manipulation (Commented) | ZetaRising | 1 | 279 | hamming distance | 461 | 0.749 | Easy | 8,187 |
https://leetcode.com/problems/hamming-distance/discuss/2655939/python | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
count = 0
while x!= 0 or y!= 0:
if ((x >> 1 << 1) != x and (y >> 1 << 1) == y) or ((x >> 1 << 1) == x and (y >> 1 << 1) != y):
count += 1
x = x >> 1
y = y >> 1
return count | hamming-distance | python | zoey513 | 1 | 64 | hamming distance | 461 | 0.749 | Easy | 8,188 |
https://leetcode.com/problems/hamming-distance/discuss/2126489/faster-than-51.58-of-Python3 | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return str(bin(x^y)).count('1') | hamming-distance | faster than 51.58% of Python3 | writemeom | 1 | 78 | hamming distance | 461 | 0.749 | Easy | 8,189 |
https://leetcode.com/problems/hamming-distance/discuss/1879804/One-Line-Solution-Python | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
return bin(x^y).count('1') | hamming-distance | One Line Solution Python | _191500221 | 1 | 41 | hamming distance | 461 | 0.749 | Easy | 8,190 |
https://leetcode.com/problems/hamming-distance/discuss/1826412/Python3-oror-Easy-to-understand | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
c = x^y #performing xor oeration
count = 0
while c > 0:#converting decimal to binary
rem = c%2
c = c//2
if rem ==1:#if we found 1 in binary we will add its occurance by one
... | hamming-distance | Python3 || Easy to understand | SV_Shriyansh | 1 | 39 | hamming distance | 461 | 0.749 | Easy | 8,191 |
https://leetcode.com/problems/hamming-distance/discuss/1752050/Python-or-Two-Solutions-or-Bit-Manipulation | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
def hammingWt(n):
count = 0
while n:
count += n&1
n >>= 1
return count
return hammingWt(x ^ y) | hamming-distance | Python | Two Solutions | Bit Manipulation | mehrotrasan16 | 1 | 106 | hamming distance | 461 | 0.749 | Easy | 8,192 |
https://leetcode.com/problems/hamming-distance/discuss/1752050/Python-or-Two-Solutions-or-Bit-Manipulation | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
nonZeros, res = x^y, 0
while nonZeros:
print(nonZeros & (nonZeros-1))
nonZeros &= (nonZeros-1)
res += 1
return res | hamming-distance | Python | Two Solutions | Bit Manipulation | mehrotrasan16 | 1 | 106 | hamming distance | 461 | 0.749 | Easy | 8,193 |
https://leetcode.com/problems/hamming-distance/discuss/1586298/Python3-two-different-Solutions | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
x_bin = bin(x)[2:]
y_bin = bin(y)[2:]
diff = len(x_bin)-len(y_bin)
if len(x_bin)>len(y_bin):
y_bin = '0'*abs(diff) + y_bin
else:
x_bin = '0'*abs(diff) + x_bin
count = 0
f... | hamming-distance | Python3 two different Solutions | light_1 | 1 | 56 | hamming distance | 461 | 0.749 | Easy | 8,194 |
https://leetcode.com/problems/hamming-distance/discuss/1586298/Python3-two-different-Solutions | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
xor = x ^ y
count = 0
for _ in range(32):
# for checking if bit is changed by xor operation or not
count += xor & 1
# for shifting bit to right side
xor = xor >> 1
re... | hamming-distance | Python3 two different Solutions | light_1 | 1 | 56 | hamming distance | 461 | 0.749 | Easy | 8,195 |
https://leetcode.com/problems/hamming-distance/discuss/1546370/Python-XOR-explained | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
result = x ^ y
return self.get_result(result)
def get_result(self, num):
result = 0
while num:
result += num & 1
num = num >> 1
return result | hamming-distance | Python XOR explained | SleeplessChallenger | 1 | 70 | hamming distance | 461 | 0.749 | Easy | 8,196 |
https://leetcode.com/problems/hamming-distance/discuss/1489490/Python-95-faster-speed | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
# 1 XOR 4 = 001 XOR 100 = 101
return bin(x^y)[2:].count('1') | hamming-distance | Python // 95% faster speed | fabioo29 | 1 | 156 | hamming distance | 461 | 0.749 | Easy | 8,197 |
https://leetcode.com/problems/hamming-distance/discuss/553450/Python3-simple-bit-manipulation%3A-XOR-%2B-clear-least-significant-bit | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
# highlight differences with XOR
tmp = x^y
# count the number of 1's in the diff
counter = 0
while tmp != 0:
# clear the least significant bit
tmp &= tmp-1
counter += 1
... | hamming-distance | Python3 simple bit manipulation: XOR + clear least significant bit | zetinator | 1 | 301 | hamming distance | 461 | 0.749 | Easy | 8,198 |
https://leetcode.com/problems/hamming-distance/discuss/2843444/easy-python-solution | class Solution:
def hammingDistance(self, x: int, y: int) -> int:
cnt = 0
for i in range(32):
if (x&1) != (y&1):
cnt+=1
x>>=1
y>>=1
return cnt | hamming-distance | easy python solution | Cosmodude | 0 | 2 | hamming distance | 461 | 0.749 | Easy | 8,199 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.