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 j+=1 else: j+=1 return c
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 i += 1 elif s[j] <= g[i]: j += 1 return countCookie
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 += 1 j += 1 return 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 i += 1 return count
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 res
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 pointer indexes # if we cannot satisfy this child, we'll never satisfy any after it with this cookie. Increment the cookie only. # repeat process until cookies are exhausted or all children with matching cookies satisfied. # can stop here since we give at most one cookie to each child. # sort for n log n + m log m time, where n is the number of children and m the number of cookies g.sort() s.sort() # set the index pointers and list sizes greedy_index = 0 satisfaction_index = 0 greed_size = len(g) sat_size = len(s) # store number satisfied number_satisfied = 0 # while in bounds while greedy_index < greed_size and satisfaction_index < sat_size : current_greed = g[greedy_index] current_cookie = s[satisfaction_index] # if this cookie can satisfy this child, increment all the pointers if current_cookie >= current_greed : number_satisfied += 1 satisfaction_index += 1 greedy_index += 1 # if this cookie cannot, increment the satisfaction index to a larger cookie else : satisfaction_index += 1 # return number satisfied overall. return number_satisfied
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 else: j += 1 return count
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 cookiei = 0 # taking variable while cookiei < len(s) and childi < len(g): # traversing elements in the list. if s[cookiei] >= g[childi]: # checking if cookies are more or equal to a child greedy. childi += 1 # if child greedy is getting fulfilled it will be content, and will increase out counter. cookiei += 1 # if child is getting the cookie, then we are increasing both else we`ll just check in cookies could we have any to fulfill at least one child greedy for that will increase only cookie return childi # returning the number of content children.
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 else: co+=1 return count
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] <= s[cookie]: ret += 1 greed += 1 cookie += 1 else: greed += 1 return ret
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 += 1 else: continue else: break return c
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() return response
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 return x
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 if s[sp] >= g[i]: ans += 1 sp-=1 return ans
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 that child? counter += 1 # increase a counter and go to the next child return counter
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: return True # always ensure stack can be popped in increasing order while stck and stck[-1] < nums[i]: second_num = stck.pop() # this will ensure second_num < stck[-1] for next iteration stck.append(nums[i]) return False
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): # If m is greater than nums[i], return true... if nums[i] < m: return True # If stack is not empty &amp; nums[i] is greater than the top element of stack, then pop the element... else: while stack and stack[-1] < nums[i]: m = stack.pop() # Otherwise, append nums[i] into stack... stack.append(nums[i]) # If the condition is not satisfied, return false. return False
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 first index... for i in range(len(nums)-1, -1, -1): # If min is greater than nums[i], return true... if nums[i] < mini: return True # If stack is not empty &amp; nums[i] is greater than the top element of stack, then pop the element... else: while stack and stack[-1] < nums[i]: mini = stack.pop() # Otherwise, append nums[i] into stack... stack.append(nums[i]) # If the condition is not satisfied, return false. return False
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]) ) stack=[] for j in range(n-1,-1,-1): # makeing stack for the nums[k] while stack and stack[-1]<=mi[j]: stack.pop() if len(stack)>0: if mi[j]<stack[-1]<nums[j]: return True stack.append(nums[j]) return False
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_list.append(nums[0]) for i in range(1,len(nums)): min_list.append(min(nums[:i])) # checking for valid patterns for j in range(len(nums) - 1, -1, -1): if nums[j] > min_list[j]: while stack and stack[-1] <= min_list[j]: stack.pop() if stack and stack[-1] < nums[j]: return True stack.append(nums[j]) return False
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 .""" while st and i>=st[-1][0]: st.pop() """ Below statement defines i>st[-1][1] i.e., we have founded a value (i) which is greater than st[-1][1] (minimum) and and smaller than st[-1][0] . Here in this below statement i acts as k (given in question) as it is greater than st[-1][1] and less than st[-1][0] . st[-1][1] acts as i(in question) and st[-1][0] acts as j(in question) as it has highest value among all.""" if st and i>st[-1][1]: return True """ Appending value and minimum value in stack. Here minn acts as i for the given question i.e., i<j<k """ st.append([i,minn]) """ Updating minimum value """ minn=min(minn,i) return False
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) return False
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 stack and nums[i] > stack[-1][1]: return True stack.append([nums[i],minVal]) # get the minimum value before the current index value minVal = min(minVal,nums[i]) return False
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, pattern_min]) pattern_min = min(pattern_min, i) return False
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 larger than current if stack: ii, xx = stack[-1] if mn[ii] < x < xx: return True # 1-3-2 order stack.append((i, x)) return False
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) return False
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] > mins[i]: while temp and temp[-1]<=mins[i]: temp.pop() if temp and temp[-1]<nums[i]: return True temp.append(nums[i]) return False
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]: # return True # Optimised using BS: O(nlogn) --> TLE # for i in range(n): # j=i+1 # k=n-1 # while j<k: # if nums[i]<nums[j] and nums[i]<nums[k]: # if nums[j]<nums[k]: # k-=1 # elif nums[k]==nums[j]: # j+=1 # else: # return True # elif nums[i]<nums[j]: # k-=1 # else: # j+=1 # Optimised using Monotonic stack: O(n) st=[] mini=nums[0] for num in nums: while st and st[-1][0]<=num: st.pop() if st and st[-1][1]<num: return True st.append([num, mini]) mini=min(mini, num) return False
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: return True stack.append([n,minCurr]) minCurr = min(n, minCurr) return False
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 and stack[-1] < nums[i]: last_pattern_element = stack.pop() stack.append(nums[i]) return False
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 increasing from bottom so pop other and keep highest valur at top. while stack and stack[-1][0] <= curr: stack.pop() # compare minLeftToStack < curr < stackTop if stack and curr < stack[-1][0] and stack[-1][1] < curr: return True #append currMin first as we want min value from left of the stackTop and then update the currMin stack.append([curr,currMin]) currMin = min(currMin, curr) return False
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(nums) -1 , -1, -1): num_1 = nums[i] if num_1 < num_3: return True #Loop to maintain num_3 < num_2 condition #Ensure Stack popping the elements in increasing order while stack and stack[-1] < num_1: num_3 = stack[-1] #this ensures num3 < num2 (i.e stack[-1]) stack.pop() stack.append(num_1) return False
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(maxi) > 0 and maxi[-1] <= mini[i]: maxi.pop() if len(maxi) > 0 and maxi[-1] < nums[i]: return True maxi.append(nums[i]) return False
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]]: stack.pop() if stack: prev_greater[i] = stack[-1] stack.append(i) mins = [0] for i in range(1, len(nums)): mins.append(mins[-1]) if nums[i] < nums[mins[-1]]: mins[-1] = i for i in range(1, len(nums)): if ((pg := prev_greater[i]) != -1 and nums[mins[pg]] < nums[i] and # nums[i] < nums[pg] by the definition of PGE mins[pg] != pg): # mins[pg] <= pg, so only need to check if mins[pg] != pg return True return False
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: break # credit to @crazyhyz, add this condition to avoid revisited visited.add(i) local_s.add(i) prev, i = i, (i + nums[i]) % n if prev == i or (nums[i] > 0) != (nums[prev] > 0): break return False
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) prev=i i=(i+nums[i])%len(nums) # index position for next element if prev==i or (nums[i]>0)!=(nums[prev]>0): # checks whether all the elements in circular subset have same sign break return False
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: break local.add(i) seen.add(i) prev=i i=(i+nums[i])%len(nums) if prev==i or (nums[i]>0)!=(nums[prev]>0): break return False
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) if next_index < 0: next_index = len(nums) - next_index if next_index == cur_index: next_index = -1 return next_index for index in range(len(nums)): is_positive = nums[index] >= 0 fast, slow, = index, index while True: slow = get_next_index(nums, slow, is_positive) fast = get_next_index(nums, fast, is_positive) if fast != -1: fast = get_next_index(nums, fast, is_positive) if slow == -1 or fast == -1: break if slow == fast: return True return False
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-solution-beats-100-with-O(1)-space offers a simple idea to identify a size-one loop: check whether the next index is the same as the current one. This way, we don't have to keep record of the link size. We can return true for all loops except those whose next index is the same as the current. We also create a marker for each traversal. Once we hit an index with the same marker of the current traversal, we know for sure that a loop of size larger than one has been found. O(N) time, O(1) space. 51 ms, 63% ranking. """ N = len(nums) for i in range(N): marker, j, direction = 1001 + i, i, nums[i] while -1000 <= nums[j] <= 1000: if direction * nums[j] > 0: # point to same direction next_j = (j + nums[j] + N) % N if next_j == j: # loop of size 1, do not assign marker break nums[j], j = marker, next_j else: break if nums[j] == marker: return True return False
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 while i < size: #if already visited , No Need to find cycle on this index if visited[i]: i += 1 continue v = i #find cycle_index origin cycle_idx = 0 for j in range(size): visited[v] = True v = (v+nums[v])%size if visited[v]: cycle_idx = v v = cycle_idx #print(i,v, nums) cycle_len = 0 if nums[v] > 0: sign = 1 else: sign = -1 #find cycle length from cycle_index and check all elements in cycle are of same sign flag_nxt = False while True: cycle_len += 1 if sign == 1 and nums[v] < 0: flag_nxt = True break if sign == -1 and nums[v] > 0: flag_nxt = True break v = (v+nums[v])%size if v == cycle_idx: break i += 1 if flag_nxt: continue #condition met for cycle if cycle_len > 1: return True return False
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[slow] * nums[fast] > 0 and nums[slow] * nums[next(fast)] > 0: if fast == slow: if slow == next(slow): break return True slow = next(slow) fast = next(next(fast)) add = i while nums[add] * nums[next(add)] > 0: tmp = add add = next(add) nums[tmp] = 0 return False
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: checked.update(seen) return False i = ni return True length = len(nums) checked = set() for i, num in enumerate(nums): if num != 0 and i not in checked and isCycle(i, num): return True return False
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 visited.add(new_index) # add new index to the visited if new_index == index: # one cycle element new_index = -1 return new_index n = len(nums) visited = set() for i in range(n): if i in visited: # no need to find cycle of visited indices continue visited.add(i) direction = nums[i] > 0 # find the starting direction slow = fast = i # find the next indices of slow and fast pointers while True: slow = find_next_index(slow, direction) fast = find_next_index(fast, direction) if fast != -1: # break early if necessary fast = find_next_index(fast, direction) if slow == -1 or fast == -1 or slow == fast: break if slow != -1 and slow == fast: return True return False
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 &amp; 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) if ii in seen or nums[ii] * x < 0 or ii == i: # no cycle seen |= temp break if ii in temp and ii != i: return True temp.add(i := ii) return False
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 the poison bucket is the last bucket... max_time = minutesToTest / minutesToDie + 1 # Initialize the required minimum number of pigs... req_pigs = 0 # To find the minimum number of pigs, find the minimum req_pigs such that Math.pow(max_time, req_pigs) >= buckets... while (max_time) ** req_pigs < buckets: # Increment until it will be greater or equals to bucket... req_pigs += 1 # Return the required minimum number of pigs... return req_pigs
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 immediately, so the poison bucket is the last bucket... max_time = minutesToTest / minutesToDie + 1 # Initialize the required minimum number of pigs... req_pigs = 0 # To find the minimum number of pigs, find the minimum req_pigs such that Math.pow(max_time, req_pigs) >= buckets... while (max_time) ** req_pigs < buckets: # Increment until it will be greater or equals to bucket... req_pigs += 1 # Return the required minimum number of pigs... return req_pigs
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) h=h*base+(ord(s[i])-ord("a")) h%=10**9+7 total[i]=h isGood=True for length in range(1,n): cnt=1 h=total[length-1] if n%length==0: # print(length,total,h,ha) for j in range(length,n,length): newH=total[j+length-1]-total[j-1]*ha[length] newH=(newH%(10**9+7)+10**9+7)%(10**9+7) if h!=newH: isGood=False # print(length,h,j,isGood,newH) if isGood==True: return True isGood=True return False
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: # mismatch found so move i to its prev index value if i > 0 i = lps[i-1] else: # i is at 0th position so lps[j] = 0 and move j ++ j += 1 def repeatedSubstringPattern(self, s: str) -> bool: n = len(s) lps = [0]*n self.computeLPS(s ,n , lps) length = lps[n-1] # get the lps value of the last index of lps aray if length > 0 and (n%(n-length)) == 0: return True else: return False
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 nex.append(j) return nex[-1] i = gen_next(s) return len(s) % (len(s) - i) == 0 and i != 0
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 if s == s[: i + 1] * num: return True return False
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]: return check_substring(string[n:], substring) else: return False for length in range(1, len(s)//2 + 1): if check_substring(s, s[:length]): return True return False
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 return True i+=1 return False
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 next[i]=j ''' If s can be constructed by substring of it, the next array of it must be like: [...,0,1,2,3,4,5,6,7,8,9] So, if length of s is divisable by the length of the substring before 0, we can conclude s consists of ... ''' if next[-1] != -1 and len(s) % (len(s) - (next[-1] + 1)) == 0: return True return False
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 div = rest // l #print(string , div ) if (string * div) == s[i+1 :]: return True 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)) == s: return True
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] return dp[-1] and dp[-1] % (len(s) - dp[-1]) == 0
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 True else: x += s[i] i += 1 return False
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 a += s[i] return False
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 bin_x, bin_y = get_bin(x), get_bin(y) res = 0 s1, s2 = len(bin_x), len(bin_y) bin_y = '0' * (s1 - s2) + bin_y return sum(bin_x[i] != bin_y[i] for i in range(s1))
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&amp;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 variable x, y, count = format(x, '0b').zfill(longer), format(y, '0b').zfill(longer), 0 for i in range(len(x)): if x[i] != y[i]: count += 1 return count # bit manipulation solution # TC O(n) SC O(1) xor = x ^ y # XOR your two integers ham = 0 # store number of 1's from the XOR operation while xor != 0: # while you have bits to check ham += xor % 2 # increase ham when a 1 is seen xor >>= 1 # right shift return ham
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 count+=1 return count
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&amp;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 &amp; (nonZeros-1)) nonZeros &amp;= (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 for i in range(len(x_bin)): if x_bin[i] != y_bin[i]: count += 1 return count
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 &amp; 1 # for shifting bit to right side xor = xor >> 1 return count
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 &amp; 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 &amp;= tmp-1 counter += 1 return counter
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&amp;1) != (y&amp;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