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/longer-contiguous-segments-of-ones-than-zeros/discuss/1229807/Simple-solution-in-python
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
count0 = count1 = max0 = max1 = 0
for c in s:
if c == '0':
count1 = 0
count0 += 1
max0 = max(max0, count0)
else:
count0 = 0
count1 += 1
max1 = max(max1, count1)
return (max1 > max0)
|
longer-contiguous-segments-of-ones-than-zeros
|
Simple solution in python
|
souravsingpardeshi
| 2
| 85
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,500
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1224894/Python3-count
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
ones = zeros = 0
for i in range(len(s)):
if i == 0 or s[i-1] != s[i]: cnt = 0
cnt += 1
if s[i] == "0": zeros = max(zeros, cnt)
else: ones = max(ones, cnt)
return ones > zeros
|
longer-contiguous-segments-of-ones-than-zeros
|
[Python3] count
|
ye15
| 2
| 61
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,501
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/2054444/Python-oneliner
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
return len(max(s.split('0'),key=len)) > len(max(s.split('1'),key=len))
|
longer-contiguous-segments-of-ones-than-zeros
|
Python oneliner
|
StikS32
| 1
| 38
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,502
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1989058/Python-beginner-friendly-solution-using-split()
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
one_seg = sorted(s.split("0"), key=len)
zero_seg = sorted(s.split("1"), key=len)
return len(one_seg[-1]) > len(zero_seg[-1])
|
longer-contiguous-segments-of-ones-than-zeros
|
Python beginner friendly solution using split()
|
alishak1999
| 1
| 40
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,503
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1736042/Python3-or-1LS
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
return len(max(s.split("0"))) > len(max(s.split("1")))
|
longer-contiguous-segments-of-ones-than-zeros
|
Python3 | 1LS
|
khalidhassan3011
| 1
| 33
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,504
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1678823/fast-python-using-fact-that-string-is-binary
|
class Solution(object):
def checkZeroOnes(self, s):
zeros = s.split("1")
ones = s.split("0")
z = len(max(zeros, key = len))
o = len(max(ones, key = len))
return o>z
```
|
longer-contiguous-segments-of-ones-than-zeros
|
fast python using fact that string is binary
|
martasapizhak
| 1
| 79
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,505
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1453291/Simple-Python-O(n)-two-pointer-solution
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
ones_len = zeros_len = 0
left = right = 0
while right < len(s)+1:
if right == len(s) or s[right] != s[left]:
if s[left] == '0':
zeros_len = max(zeros_len, right-left)
else:
ones_len = max(ones_len, right-left)
left = right
right += 1
return ones_len > zeros_len
|
longer-contiguous-segments-of-ones-than-zeros
|
Simple Python O(n) two pointer solution
|
Charlesl0129
| 1
| 105
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,506
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1453291/Simple-Python-O(n)-two-pointer-solution
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
max_ones_len = max_zeros_len = 0
cur_ones_len = cur_zeros_len = 0
for d in s:
if d == '1':
cur_ones_len += 1
cur_zeros_len = 0
else:
cur_zeros_len += 1
cur_ones_len = 0
max_ones_len = max(max_ones_len, cur_ones_len)
max_zeros_len = max(max_zeros_len, cur_zeros_len)
return max_ones_len > max_zeros_len
|
longer-contiguous-segments-of-ones-than-zeros
|
Simple Python O(n) two pointer solution
|
Charlesl0129
| 1
| 105
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,507
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1245345/Python-or-Simple-Solution
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
zeroCount = 0
oneCount = 0
contiguousOne = 0
contiguousZero = 0
for i in s:
if i == '1':
oneCount += 1
contiguousZero = max(contiguousZero, zeroCount)
zeroCount = 0
else:
zeroCount += 1
contiguousOne = max(contiguousOne, oneCount)
oneCount = 0
contiguousOne = max(contiguousOne, oneCount)
contiguousZero = max(contiguousZero, zeroCount)
return contiguousOne > contiguousZero
|
longer-contiguous-segments-of-ones-than-zeros
|
Python | Simple Solution
|
dee7
| 1
| 103
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,508
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1241395/Python3-one-line-solution-with-list-comprehension
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
return max([len(f) for f in s.split("0")]) > max([len(f) for f in s.split("1")])
|
longer-contiguous-segments-of-ones-than-zeros
|
Python3 one line solution with list comprehension
|
albezx0
| 1
| 91
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,509
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1240871/python-sol-faster-than-99-less-mem-than-90
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
ones = 0
zeros = 0
i = 0
while (i < len(s)):
newOne = 0
newZero = 0
while i < len(s) and s[i] == "1":
newOne += 1
i += 1
if (newOne > ones):
ones = newOne
while i < len(s) and s[i] == "0":
newZero += 1
i += 1
if (newZero > zeros):
zeros = newZero
return ones > zeros
|
longer-contiguous-segments-of-ones-than-zeros
|
python sol faster than 99% , less mem than 90%
|
elayan
| 1
| 71
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,510
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1238592/PythonPython3-Soultion-with-Explanation
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
cntOne,cntZero = 0,0
maxOne,maxZero = 0,0
for i in s:
if i == '0':
cntZero += 1
cntOne = 0
if maxZero < cntZero:
maxZero = cntZero
else:
cntOne += 1
cntZero = 0
if maxOne < cntOne:
maxOne = cntOne
return maxOne > maxZero
|
longer-contiguous-segments-of-ones-than-zeros
|
Python/Python3 Soultion with Explanation
|
prasanthksp1009
| 1
| 67
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,511
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1227239/Python-one-liner-using-regex.-32ms-Faster-than-100
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
return len(max(re.findall(r'1*', s))) > len(max(re.findall(r'0*', s)))
|
longer-contiguous-segments-of-ones-than-zeros
|
Python one-liner using regex. 32ms Faster than 100%
|
kingjonathan310
| 1
| 37
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,512
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1227239/Python-one-liner-using-regex.-32ms-Faster-than-100
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
return len(max(s.split('0'))) > len(max(s.split('1')))
|
longer-contiguous-segments-of-ones-than-zeros
|
Python one-liner using regex. 32ms Faster than 100%
|
kingjonathan310
| 1
| 37
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,513
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/2811101/python-oror-do-not-write-the-same-code-repeatly-oror-time%3A-O(n)-oror-space%3A-O(1)
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
maxret, lst = [0, 0], [0, 0]
for i in s:
j = ord(i) - ord("0")
lst[j], lst[1 - j] = lst[j] + 1, 0
maxret[j] = max(maxret[j], lst[j])
return maxret[0] < maxret[1]
|
longer-contiguous-segments-of-ones-than-zeros
|
python || do not write the same code repeatly || time: O(n) || space: O(1)
|
Aaron1991
| 0
| 3
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,514
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/2579298/Python-Generalised-One-Pass-Arbitrary-number-of-symbols
|
class Solution:
def checkZeroOnes(self, s: str, symbol1="1", symbol2="0") -> bool:
# make a dict to save the counts
result = collections.defaultdict(int)
# make a counter for consecutive elements
counter = 0
# make a variable to save the old character
tmp_char = s[0]
# iterate over all characters
for ch in s:
# case one or zeros
if ch == tmp_char:
# update the consecutive zeros
counter += 1
# check for maximum
result[ch] = max(result[ch], counter)
else:
# reset the counter
counter = 1
# check for maximum
result[ch] = max(result[ch], counter)
# set the tmp char
tmp_char = ch
return result[symbol1] > result[symbol2]
|
longer-contiguous-segments-of-ones-than-zeros
|
[Python] - Generalised One-Pass - Arbitrary number of symbols
|
Lucew
| 0
| 12
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,515
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/2384668/Python-or100-Faster-or-Time-and-Space-complexity-O(N)-O(1)-or-Longer-Contiguous-Segments
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
max0, max1, c0, c1 = 0, 0, 0, 0
for i in range(len(s)):
if(s[i]=='1'):
c1 += 1
c0 = 0
max1= max (max1, c1)
else:
c0 += 1
c1 = 0
max0= max(max0, c0)
return True if max1>max0 else False
|
longer-contiguous-segments-of-ones-than-zeros
|
Python |100% Faster | Time & Space complexity O(N) ,O(1) | Longer Contiguous Segments
|
NITIN_DS
| 0
| 15
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,516
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/2175444/Simplest-Approach-oror-Self-Explanatory-Solution
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
maxConsOnes = 0
maxConsZeros = 0
ones = 0
zeros = 0
length = len(s)
for i in range(length):
if s[i] == "1":
ones += 1
else:
maxConsOnes = max(maxConsOnes, ones)
ones = 0
for i in range(length):
if s[i] == "0":
zeros += 1
else:
maxConsZeros = max(maxConsZeros, zeros)
zeros = 0
maxConsOnes = max(maxConsOnes, ones)
maxConsZeros = max(maxConsZeros, zeros)
return maxConsOnes > maxConsZeros
|
longer-contiguous-segments-of-ones-than-zeros
|
Simplest Approach || Self Explanatory Solution
|
Vaibhav7860
| 0
| 33
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,517
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/2024785/Python-Clean-and-Simple!
|
class Solution:
def checkZeroOnes(self, s):
count0 = max0 = count1 = max1 = 0
for d in s:
if d == '0':
count0 += 1
max0 = max(count0, max0)
count1 = 0
elif d == '1':
count1 += 1
max1 = max(count1, max1)
count0 = 0
return max1 > max0
|
longer-contiguous-segments-of-ones-than-zeros
|
Python - Clean and Simple!
|
domthedeveloper
| 0
| 48
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,518
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/2024785/Python-Clean-and-Simple!
|
class Solution:
def checkZeroOnes(self, s):
max0 = max(len(seg) for seg in s.split('1'))
max1 = max(len(seg) for seg in s.split('0'))
return max1 > max0
|
longer-contiguous-segments-of-ones-than-zeros
|
Python - Clean and Simple!
|
domthedeveloper
| 0
| 48
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,519
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1868808/Python-dollarolution
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
o, z, m = s.split('0'), s.split('1'), 0
for i in o:
m = max(m,len(i))
for i in z:
if m < len(i)+1:
return False
return True
|
longer-contiguous-segments-of-ones-than-zeros
|
Python $olution
|
AakRay
| 0
| 40
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,520
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1857540/Easiest-Python3-Solution-oror-100-Faster-oror-No-Trickey-oror-100-Easy-to-Understand
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
ones=0
zeros=0
res_zeros=0
res_ones=0
for i in range(len(s)):
if s[i]=="1":
ones=ones+1
res_ones=max(res_ones,ones)
else:
temp=ones
res_ones=max(res_ones,temp)
ones=0
for i in range(len(s)):
if s[i]=="0":
zeros=zeros+1
res_zeros=max(res_zeros,zeros)
else:
temp=zeros
res_zeros=max(res_zeros,temp)
zeros=0
return (res_ones>res_zeros)
|
longer-contiguous-segments-of-ones-than-zeros
|
Easiest Python3 Solution || 100% Faster || No Trickey || 100% Easy to Understand
|
RatnaPriya
| 0
| 51
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,521
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1663026/Simple-Python-solution-faster-than-99.01-of-submissions
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
maxOne, maxZero, currentZeroSum, currentOneSum = 0,0,0,0
for i in s:
# currentOneSum = 0
# currentZeroSum = 0
if i == "1":
currentZeroSum = 0
currentOneSum += 1
else:
currentOneSum = 0
currentZeroSum += 1
maxOne = max(currentOneSum, maxOne)
maxZero = max(currentZeroSum, maxZero)
return maxOne > maxZero
|
longer-contiguous-segments-of-ones-than-zeros
|
Simple Python solution faster than 99.01% of submissions
|
Osereme
| 0
| 54
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,522
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1415744/Python3-32ms-Memory-Less-Than-98.42
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
ones, zeros, t1, t2 = 0, 0, 0, 0
for i in s:
if i == '1':
t1 += 1
zeros = max(zeros, t2)
t2 = 0
else:
t2 += 1
ones = max(ones, t1)
t1 = 0
if t1 != 0:
ones = max(ones, t1)
if t2 != 0:
zeros = max(zeros, t2)
return ones > zeros
|
longer-contiguous-segments-of-ones-than-zeros
|
Python3 32ms Memory Less Than 98.42%
|
Hejita
| 0
| 42
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,523
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1252525/Runtime%3A-36-ms-or-Memory-Usage%3A-14.4-MB-or-beats-42.74-of-Python3-submissions
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
l = len(s)
if l == 1:
return bool(int(s))
one = 0
zero = 0
for i in range(l):
if '0' * i in s:
if i > zero:
zero = i
if '1' * i in s:
if i > one:
one = i
return (one > zero)
|
longer-contiguous-segments-of-ones-than-zeros
|
Runtime: 36 ms | Memory Usage: 14.4 MB | beats 42.74 % of Python3 submissions
|
s13rw81
| 0
| 53
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,524
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1230174/Python3-simple-solution-faster-than-95-users
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
a,b = 0,0
for i in range(len(s)+1):
if i == len(s) or s[i] == '0':
a = max(a,b)
b = 0
else:
b += 1
return not '0'*a in s
|
longer-contiguous-segments-of-ones-than-zeros
|
Python3 simple solution faster than 95% users
|
EklavyaJoshi
| 0
| 29
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,525
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1226957/Easy-Python-Solution-with-Comments
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
from collections import Counter
# count how many 0s and 1s in the string
d1 = Counter(s)
if d1['1'] == 0:
return False
else:
#from the maximum continguous 1s
for i in range(d1['1'], 0, -1):
if '1'*i in s:
# check if continuous 0s with the same length in s, if so return False, else True
if '0'* i in s:
return False
else:
return True
#no need to check further
break
|
longer-contiguous-segments-of-ones-than-zeros
|
Easy Python Solution with Comments
|
pasca15
| 0
| 18
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,526
|
https://leetcode.com/problems/longer-contiguous-segments-of-ones-than-zeros/discuss/1642204/Python-Innovative-Solution
|
class Solution:
def checkZeroOnes(self, s: str) -> bool:
numOnes = s.count('1')
numZeros = s.count('0')
while True:
if '1'*numOnes in s:
break
else:
numOnes -= 1
while True:
if '0'*numZeros in s:
break
else:
numZeros -= 1
return numOnes > numZeros
|
longer-contiguous-segments-of-ones-than-zeros
|
Python Innovative Solution
|
ElyasGoli
| -1
| 30
|
longer contiguous segments of ones than zeros
| 1,869
| 0.603
|
Easy
| 26,527
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/1225585/Python3-Concise-binary-search-code-with-comments
|
class Solution:
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
# the speed upper is either the longest train ride: max(dist),
# or the last train ride divide by 0.01: ceil(dist[-1] / 0.01).
# notice: "hour will have at most two digits after the decimal point"
upper = max(max(dist), ceil(dist[-1] / 0.01))
#
# the function to calcute total time consumed
total = lambda speed: sum(map(lambda x: ceil(x / speed), dist[:-1])) + (dist[-1] / speed)
# the case of impossible to arrive office on time
if total(upper) > hour:
return -1
#
# binary search: find the mimimal among "all" feasible answers
left, right = 1, upper
while left < right:
mid = left + (right - left) // 2
if total(mid) > hour:
left = mid + 1 # should be larger
else:
right = mid # should explore a smaller one
return right
|
minimum-speed-to-arrive-on-time
|
[Python3] Concise binary search code with comments
|
macroway
| 4
| 322
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,528
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/1592449/Python-Binary-Search-with-Explanation
|
class Solution:
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
# helper returns required time to reach the office given a speed
def getRequiredTime(speed):
time = sum([ceil(d/speed) for d in dist[:-1]])
time += dist[-1]/speed
return time
dec = hour % 1 or 1 # decimal part of the `hour`
lo, hi = 1, ceil( max(dist) / dec ) # min and max speed
res = -1
while lo <= hi:
mid = (lo + hi) // 2
time = getRequiredTime(mid)
if time == hour:
return mid
if time < hour:
res = mid
hi = mid - 1
else:
lo = mid + 1
return res
|
minimum-speed-to-arrive-on-time
|
[Python] Binary Search with Explanation
|
artod
| 3
| 152
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,529
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/1225281/python3-binary-Search-Solution-(Explained)
|
class Solution:
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
sum_ = sum(dist)
self.ans = float("inf")
def can_reach_office(speed):
time = sum([math.ceil(dist[i]/speed) for i in range(len(dist)-1)])
return (time+(dist[-1]/speed)) <= hour
def binary_search(left,right):
while left<=right:
speed = left + (right-left)//2
if can_reach_office(speed):
self.ans = min(self.ans,speed)
right = speed - 1
else:
left = speed + 1
binary_search(1,10**7)
return -1 if self.ans == float("inf") else self.ans
|
minimum-speed-to-arrive-on-time
|
python3 binary Search Solution (Explained)
|
swap2001
| 2
| 77
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,530
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/1224905/Python3-binary-search
|
class Solution:
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
lo, hi = 1, 10_000_001
while lo < hi:
mid = lo + hi >> 1
tm = sum((dist[i]+mid-1)//mid for i in range(0, len(dist)-1)) + dist[-1]/mid
if tm <= hour: hi = mid
else: lo = mid + 1
return lo if lo < 10_000_001 else -1
|
minimum-speed-to-arrive-on-time
|
[Python3] binary search
|
ye15
| 2
| 88
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,531
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/2133912/C%2B%2B-Java-Python-Solution%3A-O(NlogN)-Time-Complexity
|
class Solution:
def check(self, arr, m):
sum = 0
for i,a in enumerate(arr):
if(math.ceil(a/m)==math.floor(a/m) or i==len(arr)-1):
sum += a/m
else:
sum += math.ceil(a/m)
return sum
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
l,r = 1, 10000000
ok = False
while l<=r:
mid = (l+r)//2
if(self.check(dist, mid)<=hour):
ok = True
r = mid-1
else:
l = mid+1
return l if ok else -1
|
minimum-speed-to-arrive-on-time
|
C++ / Java / Python Solution: O(NlogN) Time Complexity
|
deleted_user
| 1
| 62
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,532
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/2133912/C%2B%2B-Java-Python-Solution%3A-O(NlogN)-Time-Complexity
|
class Solution:
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
l,r = 1, 10000000
ok = False
while l<=r:
mid = (l+r)//2
need = dist[-1]/mid
sm = 0
for i in range(len(dist)-1):
sm += (dist[i]+mid-1)//mid
need+=sm
if(need<=hour):
ok = True
r = mid-1
else:
l = mid+1
return l if ok else -1
|
minimum-speed-to-arrive-on-time
|
C++ / Java / Python Solution: O(NlogN) Time Complexity
|
deleted_user
| 1
| 62
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,533
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/2348640/Python-3-solution-using-Binary-Search
|
class Solution:
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
if hour <= len(dist)-1:
return -1
l=1
'''Here r is set to 10**7 instead of max(r) only to satisfy the edge testcases'''
r=10**7
while(l<r):
mid= l+ (r-l)//2
sums=0
for i in dist:
'''Apply math.ceil to the previous value of sum because the question tells us that if a train ride takes 1.5 hours we have to wait an additional 0.5 hours'''
sums=math.ceil(sums)+ (i)/mid
if sums<=hour:
r=mid
else:
l=mid+1
return l
|
minimum-speed-to-arrive-on-time
|
Python 3 solution using Binary Search
|
SathvikPurushotham
| 0
| 26
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,534
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/1918980/Python-ka-Solution
|
class Solution:
def canReachWithinTime(self,dist,hour,speed):
curr_hr = 0
for i in range(len(dist)):
if i==len(dist)-1:
curr_hr+=dist[i]/speed
else:
curr_hr += math.ceil(dist[i]/speed)
return curr_hr<=hour
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
low=1
high=10**7
ans=-1
while low<=high:
mid=low+(high-low)//2
if self.canReachWithinTime(dist,hour,mid):
#could be our answer so we move left as we need to minimzie our answer.
ans=mid
high=mid-1
else:
#we need to increase our speed to reach within hours given.
low=mid+1
return ans
|
minimum-speed-to-arrive-on-time
|
Python ka Solution
|
Brillianttyagi
| 0
| 45
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,535
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/1728802/Python-Binary-Search-Solution
|
class Solution:
def canReachWithinTime(self,dist,hour,speed):
curr_hr=0
for i in range(0,len(dist)):
#if we are on last train , we need not get the ceil as there is no train ahead which will depart after integer hr, so to handle edge cases of floating point , take float only.
if i==len(dist)-1:
curr_hr+=(dist[i]/speed)
else:
#take ceil value as next train would depart after previous integer hour.
curr_hr+=math.ceil(dist[i]/speed)
return curr_hr<=hour
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
low=1
high=10**7
ans=-1
while low<=high:
mid=low+(high-low)//2
if self.canReachWithinTime(dist,hour,mid):
#could be our answer so we move left as we need to minimzie our answer.
ans=mid
high=mid-1
else:
#we need to increase our speed to reach within hours given.
low=mid+1
return ans
|
minimum-speed-to-arrive-on-time
|
Python Binary Search Solution
|
aryanagrawal2310
| 0
| 69
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,536
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/1535696/Python-O(1)-time-O(1)-space-binary-search-solution
|
class Solution:
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
if hour <= len(dist)-1:
return -1
def condition(x):
cnt = 0
n = len(dist)
for i in range(n):
if i < n-1:
f = dist[i]/x
if f == int(f):
cnt += f
else:
cnt += int(f) + 1
else: # i == n-1
f = dist[i]/x
cnt += f
return cnt
left, right = 1, 10**7
while left < right:
mid = (left + right) //2
if condition(mid) <= hour:
right = mid
else:
left = mid + 1
return left
|
minimum-speed-to-arrive-on-time
|
Python O(1) time, O(1) space binary search solution
|
byuns9334
| 0
| 110
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,537
|
https://leetcode.com/problems/minimum-speed-to-arrive-on-time/discuss/1359494/Binary-search-97-speed
|
class Solution:
def minSpeedOnTime(self, dist: List[int], hour: float) -> int:
len_dist = len(dist)
if sum(dist) <= hour:
return 1
elif len_dist - 1 > hour:
return -1
elif len_dist - 1 == int(hour):
if int(hour) == hour:
return -1
return max(max(dist), ceil(round(dist[-1] / (hour - int(hour)), 2)))
left, right = 1, max(dist)
while left + 1 < right:
middle = (left + right) // 2
travel_time = (sum(ceil(d / middle) for d in dist[:-1])
+ dist[-1] / middle)
if travel_time > hour:
left = middle
else:
right = middle
return right
|
minimum-speed-to-arrive-on-time
|
Binary search, 97% speed
|
EvgenySH
| 0
| 103
|
minimum speed to arrive on time
| 1,870
| 0.374
|
Medium
| 26,538
|
https://leetcode.com/problems/jump-game-vii/discuss/1224907/Python3-prefix-sum
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
prefix = [0, 1]
for i in range(1, len(s)):
prefix.append(prefix[-1])
lo = max(0, i-maxJump)
hi = max(0, i-minJump+1)
if s[i] == "0" and prefix[hi] - prefix[lo] > 0: prefix[-1] += 1
return prefix[-1] > prefix[-2]
|
jump-game-vii
|
[Python3] prefix sum
|
ye15
| 3
| 123
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,539
|
https://leetcode.com/problems/jump-game-vii/discuss/1224907/Python3-prefix-sum
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
queue, lo = [0], 0
for x in queue:
if x == len(s)-1: return True
for xx in range(max(lo+1, x+minJump), min(x+maxJump+1, len(s))):
if s[xx] == "0": queue.append(xx)
lo = max(lo, x + maxJump)
return False
|
jump-game-vii
|
[Python3] prefix sum
|
ye15
| 3
| 123
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,540
|
https://leetcode.com/problems/jump-game-vii/discuss/1224907/Python3-prefix-sum
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
lo = 0
can = [1] + [0]*(len(s)-1)
for i, x in enumerate(s):
if x == "0" and can[i]:
for ii in range(max(lo+1, i+minJump), min(i+maxJump+1, len(s))):
if s[ii] == "0": can[ii] = 1
lo = max(lo, i+maxJump) # key to pass
return can[len(s)-1]
|
jump-game-vii
|
[Python3] prefix sum
|
ye15
| 3
| 123
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,541
|
https://leetcode.com/problems/jump-game-vii/discuss/2610505/Clean-Fast-Python3-or-2-Pointer-or-O(n)-Time-and-Space
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
if s[-1] == '1':
return False
n, end = len(s), minJump
reach = [True] + [False] * (n - 1)
for i in range(n):
if reach[i]:
start, end = max(i + minJump, end), min(i + maxJump + 1, n)
for j in range(start, end):
if s[j] == '0':
reach[j] = True
if end == n:
return reach[-1]
return reach[-1]
|
jump-game-vii
|
Clean, Fast Python3 | 2 Pointer | O(n) Time & Space
|
ryangrayson
| 2
| 57
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,542
|
https://leetcode.com/problems/jump-game-vii/discuss/1775138/Python-easy-to-read-and-understand-or-DFSBFS
|
class Solution:
def dfs(self, s, i):
if i < 0 or i >= len(s) or s[i] != '0':
return
if i == len(s)-1:
self.ans = True
return
for jump in range(self.low, self.high+1):
self.dfs(s, i+jump)
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
self.low = minJump
self.high = maxJump
self.ans = False
self.dfs(s, 0)
return self.ans
|
jump-game-vii
|
Python easy to read and understand | DFS,BFS
|
sanial2001
| 2
| 206
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,543
|
https://leetcode.com/problems/jump-game-vii/discuss/1775138/Python-easy-to-read-and-understand-or-DFSBFS
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
q = [0]
far = 0
while q:
ind = q.pop(0)
if ind == len(s)-1:
return True
low = max(far+1, ind+minJump)
high = min(len(s)-1, ind+maxJump)
for jump in range(low, high+1):
if s[jump] == '0':
q.append(jump)
far = ind+maxJump
return False
|
jump-game-vii
|
Python easy to read and understand | DFS,BFS
|
sanial2001
| 2
| 206
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,544
|
https://leetcode.com/problems/jump-game-vii/discuss/2057612/BFS-Python-Solution-oror-80-Faster-oror-Memory-less-than-70
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
q=deque([0]) ; farthest=0
while q:
i=q.popleft()
for j in range(max(i+minJump,farthest+1), min(i+maxJump+1,len(s))):
if s[j]=='0':
q.append(j)
if j==len(s)-1: return True
farthest=i+maxJump
return False
|
jump-game-vii
|
BFS Python Solution || 80% Faster || Memory less than 70%
|
Taha-C
| 1
| 116
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,545
|
https://leetcode.com/problems/jump-game-vii/discuss/1224763/Sliding-Window-with-DP-oror-Well-Explained-oror-94-faster
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
if s[-1] == "1": return False
n = len(s)
dp = [False]*n
dp[-1] = True
tc = 0 #number of Trues between i+minJum, i+maxJump
for i in range(n-2, -1, -1):
# Subtracting number of True from last of the sliding window
if i+1+maxJump < n and dp[i+1+maxJump] == True: tc -= 1
# Adding True in the starting of the sliding window
if i+minJump < n and dp[i+minJump] == True: tc += 1
if s[i] == "1": continue
dp[i] = tc >= 1
return dp[0]
|
jump-game-vii
|
🐍 {Sliding Window with DP} || Well-Explained || 94% faster
|
abhi9Rai
| 1
| 171
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,546
|
https://leetcode.com/problems/jump-game-vii/discuss/2776661/Python-O(n)-Time-complexity-O(1)-Space-complexity-(with-explanation)
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
if s[-1] == "1": # If last one equal to "1", then just return False.
return False
if minJump <= len(s)-1 <= maxJump: # If the first jump can reach the end point, then return True.
return True
# Initialize th max index of s, the current and previous start point, end point.
Len, start, end, pre_start, pre_end = len(s), minJump, maxJump, -1, -1
while True:
if pre_start == start and pre_end == end: # If we can't go any further(we'll never reach the last index), return False.
return False
pre_start, pre_end = start, end # Update previous values.
# Search next round "0"s
for i in range(pre_start, pre_end+1):
# If s[i] equals "0", then we can jump
# and make sure that at least one "0" in the range if we jump from index i.
if s[i] == "0" and "0" in s[i + minJump:i + maxJump + 1]:
if start <= pre_end: # Update start point to a minimum value that greater than previous end point.
start = i + minJump
end = i + maxJump # Update end point to the maximum of this round.
if start <= Len-1 <= end: # If the start and end points contain the last index, return True.
return True
|
jump-game-vii
|
Python O(n) Time complexity, O(1) Space complexity (with explanation)
|
child70370636
| 0
| 9
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,547
|
https://leetcode.com/problems/jump-game-vii/discuss/2258360/Simple-Python-Solution-with-Explanation
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
if s[-1] != '0':
return False
n = len(s)
dp = [False] * n
dp[0] = True
i, idx_reached = 0, minJump
while i < n and not dp[-1]:
if dp[i] == True:
j = max(i + minJump, idx_reached)
while j < n and j <= i + maxJump:
dp[j] |= s[j] == '0'
j += 1
idx_reached = j
i += 1
return dp[-1]
|
jump-game-vii
|
Simple Python Solution with Explanation
|
atiq1589
| 0
| 71
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,548
|
https://leetcode.com/problems/jump-game-vii/discuss/1359910/Simulate-jumps-98-speed
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
if s[-1] == "1":
return False
if minJump == 1:
s = "0" + s.lstrip("0")
if s == "0":
return True
landing_idx = set(i for i, c in enumerate(s) if c == "0")
len_s_1 = len(s) - 1
current_positions = {0}
while current_positions:
new_positions = set()
for position in current_positions:
for i in range(position + minJump, position + maxJump + 1):
if i == len_s_1:
return True
elif i in landing_idx:
new_positions.add(i)
landing_idx.remove(i)
current_positions = new_positions
return False
|
jump-game-vii
|
Simulate jumps, 98% speed
|
EvgenySH
| 0
| 188
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,549
|
https://leetcode.com/problems/jump-game-vii/discuss/1242659/Clean-Python-BFS-approach
|
class Solution:
def canReach(self, s: str, minJump: int, maxJump: int) -> bool:
n=len(s)
queue=[0]
visited=set([0])
mx=0
while queue:
node=queue.pop(0)
if node==n-1:
return True
if node<0 and node>=n:
continue
for i in range(max(mx+1,node+minJump),min(node + maxJump, n - 1)+1):
if s[i]=='0' and i not in visited:
queue.append(i)
visited.add(i)
mx = max(mx, node + maxJump)
return False
|
jump-game-vii
|
Clean Python ,BFS approach
|
jaipoo
| 0
| 149
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,550
|
https://leetcode.com/problems/jump-game-vii/discuss/1224821/dp-and-sliding-window
|
class Solution:
def canReach(self, s: str, minJ: int, maxJ: int) -> bool:
n=len(s)
dp=[1]+[0]*(n-1)
ct=1
for i in range(minJ,n):
if ct and s[i]=='0':
dp[i]=1
ct+=dp[i+1-minJ]-dp[i-maxJ]*(i>=maxJ)
return dp[-1]==1
|
jump-game-vii
|
dp and sliding window
|
leetcode_dafu
| 0
| 99
|
jump game vii
| 1,871
| 0.251
|
Medium
| 26,551
|
https://leetcode.com/problems/stone-game-viii/discuss/1224872/Top-Down-and-Bottom-Up
|
class Solution:
def stoneGameVIII(self, s: List[int]) -> int:
s, res = list(accumulate(s)), 0
for i in range(len(s) - 1, 0, -1):
res = s[i] if i == len(s) - 1 else max(res, s[i] - res)
return res
|
stone-game-viii
|
Top-Down and Bottom-Up
|
votrubac
| 49
| 2,700
|
stone game viii
| 1,872
| 0.524
|
Hard
| 26,552
|
https://leetcode.com/problems/stone-game-viii/discuss/1505616/6-lines-python-O(n)-time-and-O(1)-space-fater-than-99
|
class Solution:
def stoneGameVIII(self, stones: List[int]) -> int:
s = sum(stones)
dp = s
for i in range(len(stones)-2, 0, -1):
s -= stones[i+1]
dp = max(dp, s - dp)
return dp
|
stone-game-viii
|
6 lines python - O(n) time and O(1) space, fater than 99%
|
pureme
| 1
| 170
|
stone game viii
| 1,872
| 0.524
|
Hard
| 26,553
|
https://leetcode.com/problems/stone-game-viii/discuss/1388024/Python3-Bottom-Up-O(N)-Solution-or-Very-Short
|
class Solution:
def stoneGameVIII(self, stones: List[int]) -> int:
sums, memory, n = [0], {}, len(stones)
for s in stones:
sums.append(s + sums[-1])
memory, biggest = [sums[-1]] * n, sums[-1]
for start in range(n - 2, -1, -1):
memory[start] = max(biggest, sums[start + 1] - biggest)
biggest = max(biggest, memory[start])
return memory[1]
|
stone-game-viii
|
Python3 Bottom Up O(N) Solution | Very Short
|
yiseboge
| 1
| 137
|
stone game viii
| 1,872
| 0.524
|
Hard
| 26,554
|
https://leetcode.com/problems/stone-game-viii/discuss/1225850/Python3-top-down-dp
|
class Solution:
def stoneGameVIII(self, stones: List[int]) -> int:
prefix = [0]
for x in stones: prefix.append(prefix[-1] + x)
@cache
def fn(i):
"""Return max score difference."""
if i+1 == len(stones): return prefix[-1]
return max(fn(i+1), prefix[i+1] - fn(i+1))
return fn(1)
|
stone-game-viii
|
[Python3] top-down dp
|
ye15
| 1
| 105
|
stone game viii
| 1,872
| 0.524
|
Hard
| 26,555
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1356591/Easy-Python-Solution(98.80)
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count=0
for i in range(len(s)-2):
if(s[i]!=s[i+1] and s[i]!=s[i+2] and s[i+1]!=s[i+2]):
count+=1
return count
|
substrings-of-size-three-with-distinct-characters
|
Easy Python Solution(98.80%)
|
Sneh17029
| 13
| 878
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,556
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1581918/Python-Sliding-Window-Solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
k = 3
if k > len(s):
return 0
letter_frequency = {}
count, windowStart = 0, 0
for windowEnd in range(len(s)):
if s[windowEnd] not in letter_frequency:
letter_frequency[s[windowEnd]] = 0
letter_frequency[s[windowEnd]] += 1
if windowEnd >= k - 1:
if len(letter_frequency) == k:
count+=1
letter_frequency[s[windowStart]] -= 1
if letter_frequency[s[windowStart]] ==0:
del letter_frequency[s[windowStart]]
windowStart += 1
return count
|
substrings-of-size-three-with-distinct-characters
|
[Python] - Sliding Window Solution
|
TheBatmanNinja
| 5
| 567
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,557
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1643675/Succinct-Python-solution-O(n)
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
def match(t):
return len(set(t)) == 3
return sum(map(match, zip(s, s[1:], s[2:])))
|
substrings-of-size-three-with-distinct-characters
|
Succinct Python solution, O(n)
|
emwalker
| 4
| 229
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,558
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1366864/Easy-to-understand-Python3-faster-than-99.83-of-Python3
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
counter = 0
for i in range(len(s)-2):
new = s[i:i+3]
if len(new) == len(set(new)):
counter = counter + 1
return counter
|
substrings-of-size-three-with-distinct-characters
|
Easy to understand Python3 - faster than 99.83% of Python3
|
cjenn26
| 2
| 74
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,559
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2810964/Python-oror-Sliding-Window-oror-95.01-Faster-oror-5-Lines
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
c,n=0,len(s)
for i in range(n-2):
t=set(s[i:i+3])
if len(t)==3:
c+=1
return c
|
substrings-of-size-three-with-distinct-characters
|
Python || Sliding Window || 95.01% Faster || 5 Lines
|
DareDevil_007
| 1
| 85
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,560
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1992350/Python-Solution-%2B-One-Liner!
|
class Solution:
def countGoodSubstrings(self, s):
count = 0
for i in range(2, len(s)):
if len(set(s[i-2:i+1])) == 3:
count += 1
return count
|
substrings-of-size-three-with-distinct-characters
|
Python - Solution + One-Liner!
|
domthedeveloper
| 1
| 185
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,561
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1992350/Python-Solution-%2B-One-Liner!
|
class Solution:
def countGoodSubstrings(self, s):
return sum(len(set(s[i-2:i+1])) == 3 for i in range(2, len(s)))
|
substrings-of-size-three-with-distinct-characters
|
Python - Solution + One-Liner!
|
domthedeveloper
| 1
| 185
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,562
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1943515/Python-or-Concise-Solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count = 0
currString = ""
for i in range(0, len(s) - 2):
currString = s[i:i+3]
if len(set(currString)) == len(currString):
count += 1
currString = ""
return count
|
substrings-of-size-three-with-distinct-characters
|
[Python] | Concise Solution
|
i_architect
| 1
| 92
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,563
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1891630/Python-easy-solution-faster-than-85
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count = 0
for i in range(len(s)-2):
if len(set(s[i:i+3])) == len(s[i:i+3]):
count += 1
return count
|
substrings-of-size-three-with-distinct-characters
|
Python easy solution faster than 85%
|
alishak1999
| 1
| 70
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,564
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1803891/1-Line-Python-Solution-oror-82-Faster-oror-Memory-less-than-80
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
return sum([len(set(s[i:i+3]))==len(s[i:i+3]) for i in range(len(s)-2)])
|
substrings-of-size-three-with-distinct-characters
|
1-Line Python Solution || 82% Faster || Memory less than 80%
|
Taha-C
| 1
| 47
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,565
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1239037/Python3-1-line
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
return sum(len(set(s[i-3:i])) == 3 for i in range(3, len(s)+1))
|
substrings-of-size-three-with-distinct-characters
|
[Python3] 1-line
|
ye15
| 1
| 45
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,566
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1239037/Python3-1-line
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
ans = 0
for i in range(3, len(s)+1):
if len(set(s[i-3:i])) == 3: ans += 1
return ans
|
substrings-of-size-three-with-distinct-characters
|
[Python3] 1-line
|
ye15
| 1
| 45
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,567
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1239037/Python3-1-line
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
ans = 0
freq = {}
for i, x in enumerate(s):
freq[x] = 1 + freq.get(x, 0)
if i >= 3:
freq[s[i-3]] -= 1
if freq[s[i-3]] == 0: freq.pop(s[i-3])
if len(freq) == 3: ans += 1
return ans
|
substrings-of-size-three-with-distinct-characters
|
[Python3] 1-line
|
ye15
| 1
| 45
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,568
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1238779/Simple-Python3-Straight-Forward-Solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
cnt = 0
for i in range(2, len(s)):
if s[i] != s[i-1] and s[i] != s[i-2] and s[i-1] != s[i-2]:
cnt += 1
return cnt
|
substrings-of-size-three-with-distinct-characters
|
Simple Python3 Straight Forward Solution
|
pandeyrishabh40
| 1
| 50
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,569
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2837251/Sliding-Window-%2B-Set-Easy-to-Understand.
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
good_strings = 0
if len(s) <= 2:
return 0
fp = 0
lp = 3
for fp in range(len(s)-2):
small_string = s[fp:lp]
print(small_string)
if len(set(small_string)) == 3:
good_strings +=1
lp += 1
return good_strings
|
substrings-of-size-three-with-distinct-characters
|
Sliding Window + Set, Easy to Understand.
|
mephiticfire
| 0
| 1
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,570
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2821800/Simple-Sliding-window-technique
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
if len(s)<3:
return 0
i=0
j=0
subString = ""
count =0
while(j<len(s)):
subString=subString+s[j]
print(subString)
if (j-i+1<3):
j+=1
else:
if(j-i+1==3):
print(subString)
if(len(set(subString))==3):
count+=1
subString=subString[1:]
i+=1
j+=1
return count
|
substrings-of-size-three-with-distinct-characters
|
Simple Sliding window technique
|
rhi_1
| 0
| 3
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,571
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2813591/Beginner-friendly-with-explanation-(Sliding-Window)
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
i = 0
countOfGoodSubStr = 0
k = 3
j = k
while j <= len(s):
x = s[i:j]
if len(set(x)) == k:
countOfGoodSubStr += 1
i += 1
j += 1
return countOfGoodSubStr
|
substrings-of-size-three-with-distinct-characters
|
Beginner friendly with explanation (Sliding Window)
|
karanvirsagar98
| 0
| 2
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,572
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2805778/Python-Fast-solution-using-a-set
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
ans = 0
for i in range(len(s) - 2):
ans += len(set(s[i:i + 3])) == 3
return ans
|
substrings-of-size-three-with-distinct-characters
|
[Python] Fast solution using a set
|
Mark_computer
| 0
| 1
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,573
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2805778/Python-Fast-solution-using-a-set
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
return sum(len(set(s[i:i + 3])) == 3 for i in range(len(s) - 2))
|
substrings-of-size-three-with-distinct-characters
|
[Python] Fast solution using a set
|
Mark_computer
| 0
| 1
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,574
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2797389/Python3-simpler-code-for-Good-substring
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
n = len(s)
k=3
good = 0
for i in range(n-k+1):
string = s[i:k+i]
unique = set(string) //Used to find unique elements in a string
if len(unique) == 3:
good+=1
return good
|
substrings-of-size-three-with-distinct-characters
|
Python3 simpler code for Good substring
|
sukumaran1004
| 0
| 1
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,575
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2778185/Stack-Python-solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
if len(s) < 3:
return 0
stack = [s[0], s[1]]
ans = 0
for i in s[2:]:
stack.append(i)
if len(stack) == 3 and len(set(stack)) == len(stack):
ans +=1
stack.pop(0)
return ans
|
substrings-of-size-three-with-distinct-characters
|
Stack Python solution
|
kruzhilkin
| 0
| 2
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,576
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2719817/Sliding-window-O(1)-time-solution-for-flexible-substring-size-(k-not-always-3)
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
k = 3
if len(s) < k:
return 0
left, right = 0, 0
chars = {}
while right < k:
right_char = s[right]
chars[right_char] = chars[right_char] + 1 if right_char in chars else 1
right += 1
result = 1 if len(chars) == k else 0
while right < len(s):
left_char = s[left]
right_char = s[right]
chars[left_char] -= 1
if chars[s[left]] == 0:
del chars[s[left]]
chars[right_char] = chars[right_char] + 1 if right_char in chars else 1
if len(chars) == k:
result += 1
left += 1
right += 1
return result
|
substrings-of-size-three-with-distinct-characters
|
Sliding window O(1) time solution for flexible substring size (k not always 3)
|
kudratkhujamusaev
| 0
| 5
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,577
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2712055/pyhton-simple-solution-faster
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
res=0
for i in range(2,len(s)):
d=s[i]+s[i-1]+s[i-2]
if(len(set(d))==len(d)):
res+=1
return res
|
substrings-of-size-three-with-distinct-characters
|
pyhton simple solution faster
|
Raghunath_Reddy
| 0
| 2
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,578
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2505010/Simplest-python-solution.
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count = 0
for i in range(len(s)-2):
if len(set([s[i],s[i+1],s[i+2]])) == 3:
count+=1
return count
|
substrings-of-size-three-with-distinct-characters
|
Simplest python solution.
|
EbrahimMG
| 0
| 34
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,579
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2504658/Simple-Python-solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
res = 0
while(len(s) >= 3):
temp = s[:3]
if len(set(temp)) == len(temp):
res += 1
s = s[1:]
return res
|
substrings-of-size-three-with-distinct-characters
|
Simple Python solution
|
Wartem
| 0
| 16
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,580
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2499198/python-solution-or-sliding-window
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
i=j=0
n=len(s)
temp=[]
ans=0
while j<n:
temp.append(s[j])
if j-i+1<3:
j+=1
elif j-i+1==3:
if len(temp)==len(set(temp)):
ans+=1
temp.remove(temp[0])
i+=1
j+=1
return ans
|
substrings-of-size-three-with-distinct-characters
|
python solution | sliding window
|
ayushigupta2409
| 0
| 24
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,581
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2462041/Python3-Sliding-Window-Clean-Solution-beats-98
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
front = back = count = 0
while back < len(s):
if back - front + 1 == 3:
if s[front] != s[front+1] and s[front+1]!= s[front+2] and s[front] != s[front+2]:
count += 1
front += 1
back += 1
return count
|
substrings-of-size-three-with-distinct-characters
|
Python3 Sliding Window Clean Solution beats 98 %
|
abhisheksanwal745
| 0
| 19
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,582
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2427296/Using-set-and-array-indexing-in-Python
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
sb = []
for i in range(len(s)):
if i + 3 > len(s):
break
sb.append(s[i: i + 3])
res = []
for w in sb:
if len(set(w)) == len(w):
res.append(w)
return len(res)
|
substrings-of-size-three-with-distinct-characters
|
Using set and array indexing in Python
|
ankurbhambri
| 0
| 20
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,583
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2418613/Python-Easy-solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count=0
for i in range(len(s)):
s1=set(s[i:i+3])
if(len(s1)==3):
count+=1
return count
|
substrings-of-size-three-with-distinct-characters
|
Python Easy solution
|
mrigank2303239
| 0
| 21
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,584
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2377498/Python-Solution-or-One-Liner-or-Kind-Of-Sliding-Window
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
return sum(len(set(s[i:i+3])) == 3 for i in range(0,len(s)-2))
|
substrings-of-size-three-with-distinct-characters
|
Python Solution | One - Liner | Kind Of Sliding Window
|
Gautam_ProMax
| 0
| 26
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,585
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2283733/Simple-python-solution-with-18ms-runtime
|
class Solution(object):
def isValid(self, temp):
thedict = {}
for i in range(len(temp)):
if temp[i] not in thedict:
thedict[temp[i]] = 0
else:
return False
return True
def countGoodSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
if len(s) <= 2:
return 0
res = []
temp = ""
for i in range(3):
temp += s[i]
if self.isValid(temp):
res.append(temp)
for i in range(3, len(s)):
temp = temp[1:] + s[i]
if len(temp) == 3:
if self.isValid(temp):
res.append(temp)
return len(res)
|
substrings-of-size-three-with-distinct-characters
|
Simple python solution with 18ms runtime
|
Gilbert770
| 0
| 25
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,586
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2184730/Python-simple-solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
ans = 0
for i in range(3,len(s)+1):
if len(set(s[i-3:i])) == 3:
ans += 1
return ans
|
substrings-of-size-three-with-distinct-characters
|
Python simple solution
|
StikS32
| 0
| 34
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,587
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2174846/FASTEST-SOLUTION-100-faster-than-python-solutions
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
window_size = 3 # static window
result = 0
if len(s) >= window_size:
for start in range(len(s) - window_size + 1):
result += (1 if (len(set(s[start:start + window_size])) == 3) else 0)
return result
|
substrings-of-size-three-with-distinct-characters
|
FASTEST SOLUTION 100% faster than python solutions
|
notxkaran
| 0
| 67
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,588
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/2079596/Basic-sliding-window
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
res = i = 0
while i <= len(s) - 3:
sub = set(s[i:i+3])
if len(sub) == 3:
res += 1
i += 1
return res
|
substrings-of-size-three-with-distinct-characters
|
Basic sliding window
|
andrewnerdimo
| 0
| 104
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,589
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1954159/Python3-simple-solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count = 0
for i in range(len(s)-2):
if s[i] != s[i+1] and s[i] != s[i+2] and s[i+1] != s[i+2]:
count += 1
return count
|
substrings-of-size-three-with-distinct-characters
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 27
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,590
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1927232/Python-3-Solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
c = 0
i = 0
j = 1
word = ""
while j < len(s):
if j - i + 1 < 3:
word += s[i]
if s[j] not in word:
word += s[j]
j += 1
else:
if s[j] not in word:
word += s[j]
if len(word) == 3:
c += 1
word = ""
i += 1
return c
|
substrings-of-size-three-with-distinct-characters
|
Python 3 Solution
|
AprDev2011
| 0
| 47
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,591
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1868883/Python-dollarolution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count = 0
for i in range(len(s)-2):
if len(set((s[i],s[i+1],s[i+2]))) == 3:
count += 1
return count
|
substrings-of-size-three-with-distinct-characters
|
Python $olution
|
AakRay
| 0
| 37
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,592
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1775898/Simple-and-Easy-to-understand-solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
ans1=[]
for i in range(len(s)-2):
ans=""
ans+=s[i:i+3]
ans1.append(ans)
count=0
for a in ans1:
if len(set(a)) ==3:
count+=1
return count
|
substrings-of-size-three-with-distinct-characters
|
Simple and Easy to understand solution
|
Buyanjargal
| 0
| 49
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,593
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1621512/Python.-Easy-and-Fast.-24-36ms-runtime.
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
i = 0
goodStrings = 0
while i < len(s)-2:
currSet = set(s[i:i+3])
if len(currSet) == 3:
goodStrings +=1
i+=1
elif len(currSet) == 1:
i+=2
else:
i+=1
return goodStrings
|
substrings-of-size-three-with-distinct-characters
|
Python. Easy and Fast. 24-36ms runtime.
|
manassehkola
| 0
| 55
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,594
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1595825/Simple-Python-Solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
ans = 0
if len(s) < 3: return ans
slow = 0
fast = slow+2
while fast < len(s):
if len(Counter(s[slow:fast+1])) == 3:
ans += 1
fast += 1
slow += 1
return ans
|
substrings-of-size-three-with-distinct-characters
|
Simple Python Solution
|
anandudit
| 0
| 49
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,595
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1496372/2-solutions-in-Python-one-liner-and-sliding-window
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
return sum(len(set(s[i:(i + 3)])) == 3 for i in range(len(s) - 2))
|
substrings-of-size-three-with-distinct-characters
|
2 solutions in Python, one-liner and sliding-window
|
mousun224
| 0
| 129
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,596
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1496372/2-solutions-in-Python-one-liner-and-sliding-window
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
cnt = j = 0
seen = dict()
for i, c in enumerate(s):
if c in seen and i - seen[c] < 3:
j = seen[c] + 1
elif i - j == 2:
cnt += 1
j += 1
seen[c] = i
return cnt
|
substrings-of-size-three-with-distinct-characters
|
2 solutions in Python, one-liner and sliding-window
|
mousun224
| 0
| 129
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,597
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1493872/python-one-liner-O(n)-solution-with-zip
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
return sum(a1 != a2 != a3 != a1 for a1, a2, a3 in zip(s, s[1:], s[2:]))
|
substrings-of-size-three-with-distinct-characters
|
[python] one-liner O(n) solution with zip
|
licpotis
| 0
| 35
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,598
|
https://leetcode.com/problems/substrings-of-size-three-with-distinct-characters/discuss/1389313/Simple-Python-Solution
|
class Solution:
def countGoodSubstrings(self, s: str) -> int:
count=0
for i in range(len(s)-2):
ele=s[i:i+3]
if len(set(ele))==3:
count+=1
return count
|
substrings-of-size-three-with-distinct-characters
|
Simple Python Solution
|
sangam92
| 0
| 36
|
substrings of size three with distinct characters
| 1,876
| 0.703
|
Easy
| 26,599
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.