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/is-subsequence/discuss/2783712/Slow-and-Fast-Pointer-Python-3 | class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
slow_ptr = 0
fast_ptr = 0
while slow_ptr < len(s) and fast_ptr < len(t):
if s[slow_ptr] == t[fast_ptr]:
slow_ptr += 1
fast_ptr += 1
else:
fast_ptr += 1
... | is-subsequence | Slow and Fast Pointer [Python 3] | thchong-code | 0 | 1 | is subsequence | 392 | 0.49 | Easy | 6,800 |
https://leetcode.com/problems/is-subsequence/discuss/2780761/Python-clear-2-pointers-O(n) | class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
p_s, p_t = 0, 0
while p_t < len(t) and p_s < len(s):
if s[p_s] == t[p_t]:
p_s += 1
p_t += 1
if p_s == len(s):
return True
return False | is-subsequence | Python, clear 2-pointers, O(n) ✅ | Gagampy | 0 | 1 | is subsequence | 392 | 0.49 | Easy | 6,801 |
https://leetcode.com/problems/is-subsequence/discuss/2779538/Python3-or-Speedy | class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) == 0: #edge case for ""
return True
indx = 0
sarr = [0 for x in range(len(s))] #create array size s
for st in t: #iterate through t
... | is-subsequence | Python3 | Speedy | vmb004 | 0 | 4 | is subsequence | 392 | 0.49 | Easy | 6,802 |
https://leetcode.com/problems/is-subsequence/discuss/2773748/Python3-Runtime%3A-33-ms-faster-than-94.34.-Memory-Usage%3A-13.8-MB-less-than-81.67 | class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
max_idx = -1
for si in s:
cur_idx = t.find(si)
if (cur_idx==-1) or (max_idx > cur_idx):
return False
max_idx = cur_idx
t = "_"*cur_idx+t[cur_idx+1:]
else:
... | is-subsequence | [Python3] Runtime: 33 ms, faster than 94.34%. Memory Usage: 13.8 MB, less than 81.67% | huiseom | 0 | 2 | is subsequence | 392 | 0.49 | Easy | 6,803 |
https://leetcode.com/problems/is-subsequence/discuss/2771515/Simple-Fast-python-solution | class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
counter = 0
for c in s:
if c in t:
t = t[t.index(c) + 1:]
counter += 1
if len(s) == counter:
return True
return False | is-subsequence | Simple-Fast python solution | don_masih | 0 | 1 | is subsequence | 392 | 0.49 | Easy | 6,804 |
https://leetcode.com/problems/is-subsequence/discuss/2769787/Python3-Easy-solution-with-explanation | class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if len(s) > len(t):
return False
if len(s) == 0:
return True
l=0
r=0
while(l!=len(s) and r!=len(t)):
if s[l] == t[r]:
l += 1
r += 1
... | is-subsequence | Python3 Easy solution with explanation | sakthikavincit | 0 | 3 | is subsequence | 392 | 0.49 | Easy | 6,805 |
https://leetcode.com/problems/is-subsequence/discuss/2751738/Simple-Python-Solution | class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
for i in range(len(s)):
if s[i] in t:
t = t[t.index(s[i])+1:]
else:
return False
return True | is-subsequence | Simple Python Solution | yas_hh | 0 | 2 | is subsequence | 392 | 0.49 | Easy | 6,806 |
https://leetcode.com/problems/utf-8-validation/discuss/2568848/Python3-or-DP-or-Memoization-or-Neat-Solution-or-O(n) | class Solution:
def validUtf8(self, data: List[int]) -> bool:
n = len(data)
l = [2**i for i in range(7, -1, -1)]
def isXByteSeq(pos, X):
f = data[pos]
rem = data[pos+1:pos+X]
ret = (f&l[X]) == 0
for i in range(X):
r... | utf-8-validation | Python3 | DP | Memoization | Neat Solution | O(n) | DheerajGadwala | 4 | 497 | utf 8 validation | 393 | 0.452 | Medium | 6,807 |
https://leetcode.com/problems/utf-8-validation/discuss/2577038/Python3-oror-10-lines-binary-pad-wexplanation-oror-TM%3A-9597 | class Solution:
def validUtf8(self, data: List[int]) -> bool:
count = 0 # Keep a tally of non-first bytes required
for byte in data: # Pad out bytes to nine digits and ignore the 1st 1
byte|= 256 ... | utf-8-validation | Python3 || 10 lines, binary pad, w/explanation || T/M: 95%/97% | warrenruud | 3 | 55 | utf 8 validation | 393 | 0.452 | Medium | 6,808 |
https://leetcode.com/problems/utf-8-validation/discuss/1380903/python-faster-100 | class Solution:
def validUtf8(self, data: List[int]) -> bool:
eighth_bit = 1 << 7
seventh_bit = 1 << 6
sixth_bit = 1 << 5
fifth_bit = 1 << 4
fourth_bit = 1 << 3
trailing_byte_count = 0
for byte in data:
if trailing_byte_count > 0:
... | utf-8-validation | python faster 100% | Hemal-Mamtora | 3 | 518 | utf 8 validation | 393 | 0.452 | Medium | 6,809 |
https://leetcode.com/problems/utf-8-validation/discuss/825305/Python3-straightforward-solution | class Solution:
def validUtf8(self, data: List[int]) -> bool:
cnt = 0
for x in data:
x = bin(x)[2:].zfill(8)
if cnt: # in the middle of multi-byte
if x.startswith("10"): cnt -= 1
else: return False
else: # beginning
... | utf-8-validation | [Python3] straightforward solution | ye15 | 3 | 242 | utf 8 validation | 393 | 0.452 | Medium | 6,810 |
https://leetcode.com/problems/utf-8-validation/discuss/2569769/Simple-Python-Solution-oror-O(n)-Complexity | class Solution:
def validUtf8(self, data: List[int]) -> bool:
l = []
for i in range(len(data)):
l.append(bin(data[i])[2:])
if(len(l[i]) < 8):
l[i] = '0'*(8-len(l[i]))+l[i]
curr = 0
byte = 0
flag = True
for i in range(le... | utf-8-validation | Simple Python Solution || O(n) Complexity | urmil_kalaria | 1 | 93 | utf 8 validation | 393 | 0.452 | Medium | 6,811 |
https://leetcode.com/problems/utf-8-validation/discuss/2568974/Easy-python-solution | class Solution:
def validUtf8(self, data: List[int]) -> bool:
unicode=[]
for i in range(len(data)):
x=bin(data[i]).replace("0b", "")
if len(x)<8:
x='0'*(8-len(x))+x
unicode.append(x)
curr=None
cont=0
for i in range(len(unico... | utf-8-validation | Easy python solution | shubham_1307 | 1 | 282 | utf 8 validation | 393 | 0.452 | Medium | 6,812 |
https://leetcode.com/problems/utf-8-validation/discuss/2579204/Python-3-oror-136-ms-faster-than-83.28-of-Python3 | class Solution:
def validUtf8(self, data: List[int]) -> bool:
i = 0
l = len(data)
while (i < l):
bin_rep = format(data[i], '#010b')[-8:]
if '0' not in bin_rep:
return False
count = bin_rep.index('0')
if count == 0:
i += 1
continue
if count == 1 or count > 4:
return False
i +=... | utf-8-validation | Python 3 || 136 ms, faster than 83.28% of Python3 | sagarhasan273 | 0 | 15 | utf 8 validation | 393 | 0.452 | Medium | 6,813 |
https://leetcode.com/problems/utf-8-validation/discuss/2574468/OFCOURSE-NOT-THE-FASTEST-BUT-THE-EASIEST-METHOD | class Solution:
def help(self,ans,j):
if ans[j][0:5]=='11110':
if len(ans[j::])<4: return False
if ans[j+1][0:2]=='10' and ans[j+2][0:2]=='10' and ans[j+3][0:2]=='10':
if j+4<len(ans):
return self.help(ans,j+4)
else:
... | utf-8-validation | OFCOURSE NOT THE FASTEST BUT THE EASIEST METHOD | DG-Problemsolver | 0 | 8 | utf 8 validation | 393 | 0.452 | Medium | 6,814 |
https://leetcode.com/problems/utf-8-validation/discuss/2572903/Python3-Straightforward-solution | class Solution:
def validUtf8(self, data: List[int]) -> bool:
case = dict()
case["11110"] = ["10","10","10"]
case["1110"] = ["10","10"]
case["110"] = ["10"]
case["0"] = []
i = 0
while i < len(data):
x = "{0:08b}".format(data[i])
if x[:5... | utf-8-validation | [Python3] Straightforward solution | Saitama1v1 | 0 | 13 | utf 8 validation | 393 | 0.452 | Medium | 6,815 |
https://leetcode.com/problems/utf-8-validation/discuss/2571057/Python3-One-Pass | class Solution:
def validUtf8(self, data: List[int]) -> bool:
pt, num = 0, len(data)
while pt < num:
if data[pt] < 128:
pt += 1
elif 192 <= data[pt] < 224:
if pt+2>num or not all(128<=data[i]<192 for i in range(pt+1,pt+2)):
... | utf-8-validation | [Python3] One-Pass | ruosengao | 0 | 29 | utf 8 validation | 393 | 0.452 | Medium | 6,816 |
https://leetcode.com/problems/utf-8-validation/discuss/2571014/Easy-Python-O(n)-approach-(99.56) | class Solution:
def validUtf8(self, data: List[int]) -> bool:
# check if it is formed 10xxxxxx
def is_continuation(num):
return num >= 128 and num <= 191
i = 0
while i < len(data):
# 1-byte UTF8 character
if data[i] <= 127:
... | utf-8-validation | Easy Python O(n) approach (99.56%) | MajimaAyano | 0 | 56 | utf 8 validation | 393 | 0.452 | Medium | 6,817 |
https://leetcode.com/problems/utf-8-validation/discuss/2569779/GolangPython-2-solutions | class Solution:
def validUtf8(self, data: List[int]) -> bool:
i=0
while i<len(data):
bin_rep = bin(data[i])[2:].rjust(8,"0")
number_of_ones = 0
for b in bin_rep:
if b == "0":
break
number_of_ones+=1
i... | utf-8-validation | Golang/Python 2 solutions | vtalantsev | 0 | 35 | utf 8 validation | 393 | 0.452 | Medium | 6,818 |
https://leetcode.com/problems/utf-8-validation/discuss/2569779/GolangPython-2-solutions | class Solution:
def validUtf8(self, data: List[int]) -> bool:
mask1 = 128
mask2 = 192
mask3 = 224
mask4 = 240
mask5 = 248
i = 0
while i < len(data):
if data[i]&mask5 == mask4:
added = 3
elif data[i]&mask... | utf-8-validation | Golang/Python 2 solutions | vtalantsev | 0 | 35 | utf 8 validation | 393 | 0.452 | Medium | 6,819 |
https://leetcode.com/problems/utf-8-validation/discuss/2569774/Python-Shift-register-counting-or-faster-than-95 | class Solution:
def validUtf8(self, data: List[int]) -> bool:
idx = 0
while idx < len(data):
buf = data[idx]
n = 1
while buf & 0x80 != 0:
buf <<= 1
n += 1
if n == 1:
idx += 1
continue... | utf-8-validation | [Python] Shift register counting | faster than 95% | staytime | 0 | 17 | utf 8 validation | 393 | 0.452 | Medium | 6,820 |
https://leetcode.com/problems/utf-8-validation/discuss/2569448/Only-used-Conditional-Statements-Python | class Solution:
def validUtf8(self, data: List[int]) -> bool:
# one = [127, 0]
# two = [[223, 192], [191, 128]]
# three = [[239, 224], [191, 128], [191, 128]]
# four = [[247, 240], [191, 128], [191, 128], [191, 128]]
ptr = 0
while ptr<len(data):
if data[pt... | utf-8-validation | Only used Conditional Statements Python | Sahil1729 | 0 | 16 | utf 8 validation | 393 | 0.452 | Medium | 6,821 |
https://leetcode.com/problems/utf-8-validation/discuss/2569254/Python-or-Easy-to-read-nothing-fancy | class Solution:
def validUtf8(self, data: List[int]) -> bool:
n = len(data)
i = 0
while i < n:
if data[i] < 128: # 0xxxxxxx
k = 1
elif 192 <= data[i] < 224: # 110xxxxx
k = 2
elif 224 <= data[i] < 240: # 1110xxxx
... | utf-8-validation | Python | Easy to read, nothing fancy | sr_vrd | 0 | 43 | utf 8 validation | 393 | 0.452 | Medium | 6,822 |
https://leetcode.com/problems/utf-8-validation/discuss/2569203/python3-bitwise-checking-sol-for-reference | class Solution:
def checkLength(self, d):
cnt = -1
for i in range(7,0,-1):
if d & (1 << i):
cnt += 1
else:
return cnt
return cnt
def validUtf8(self, data: List[int]) -> bool:
didx = 0
... | utf-8-validation | [python3] bitwise checking sol for reference | vadhri_venkat | 0 | 15 | utf 8 validation | 393 | 0.452 | Medium | 6,823 |
https://leetcode.com/problems/utf-8-validation/discuss/2569119/python-bit-manipulation | class Solution(object):
def validUtf8(self, data):
"""
:type data: List[int]
:rtype: bool
"""
n_bit = 0
for entry in data:
# check if it is 1 byte data
if n_bit == 0:
bit_7 = entry >> 7
if bit_7 == 0:
... | utf-8-validation | python - bit manipulation | user2354hl | 0 | 15 | utf 8 validation | 393 | 0.452 | Medium | 6,824 |
https://leetcode.com/problems/utf-8-validation/discuss/2569108/Python-solution | class Solution:
def validUtf8(self, data: List[int]) -> bool:
byte = 0
for num in data:
if (byte >= 1):
if(num >> 6) == 0b10: byte -=1
else: return False
elif(num >> 3) == 0b11110: byte = 3
elif(num >> 4) == 0b1110: byte = 2
... | utf-8-validation | Python solution | chh3chan | 0 | 38 | utf 8 validation | 393 | 0.452 | Medium | 6,825 |
https://leetcode.com/problems/utf-8-validation/discuss/2569030/Python-Accepted | class Solution:
def validUtf8(self, data: List[int]) -> bool:
def length(n):
return len('{:08b}'.format(n).split('0', 1)[0])
i = 0
while i < len(data):
le = length(data[i])
i += 1
if le == 1 or le > 4:
return False
... | utf-8-validation | Python Accepted ✅ | Khacker | 0 | 50 | utf 8 validation | 393 | 0.452 | Medium | 6,826 |
https://leetcode.com/problems/utf-8-validation/discuss/2569020/Python-solution-(explanation-behind-logic-provided) | class Solution:
def validUtf8(self, data: List[int]) -> bool:
'''
1. Convert integer to the required format by removing the '0b' and fill it with preceding zeroes if necessary
2. Count number of ones at the start of the string
2a. If there is only 1 one, or there are more than 4 ones, it is invalid (return fa... | utf-8-validation | Python solution (explanation behind logic provided) | chkmcnugget | 0 | 20 | utf 8 validation | 393 | 0.452 | Medium | 6,827 |
https://leetcode.com/problems/utf-8-validation/discuss/2569014/Simple-python3-solution | class Solution:
# O(n) time,
# O(1) space,
# Approach: array,
def validUtf8(self, data: List[int]) -> bool:
n = len(data)
def findNumBytes(num: int) -> int:
if num < 128:
return 1
if num >= 192 and num < 224:
r... | utf-8-validation | Simple python3 solution | destifo | 0 | 16 | utf 8 validation | 393 | 0.452 | Medium | 6,828 |
https://leetcode.com/problems/utf-8-validation/discuss/2568920/EASY-PYTHON3-SOLUTION | class Solution:
def validUtf8(self, data: List[int]) -> bool:
n = len(data)
l = [2**i for i in range(7, -1, -1)]
def isXByteSeq(pos, X):
f = data[pos]
rem = data[pos+1:pos+X]
ret = (f&l[X]) == 0
for i in range(X):
r... | utf-8-validation | ✅✔ EASY PYTHON3 SOLUTION ✅✔ | rajukommula | 0 | 42 | utf 8 validation | 393 | 0.452 | Medium | 6,829 |
https://leetcode.com/problems/utf-8-validation/discuss/1827360/Python-soln | class Solution:
def validUtf8(self, data: List[int]) -> bool:
i=0
while i<len(data):
x=bin(data[i])[2:]
if len(x)!=8:
#Means it's a 1 byte
i+=1
continue
cnt=0
j=0
while j<len(x) and x[j]=='1':... | utf-8-validation | Python soln | heckt27 | 0 | 81 | utf 8 validation | 393 | 0.452 | Medium | 6,830 |
https://leetcode.com/problems/decode-string/discuss/1400105/98-faster-oror-With-and-without-Stack-oror-Cleane-and-Concise | class Solution:
def decodeString(self, s: str) -> str:
res,num = "",0
st = []
for c in s:
if c.isdigit():
num = num*10+int(c)
elif c=="[":
st.append(res)
st.append(num)
res=""
num=0
elif c=="]":
pnum = s... | decode-string | 🐍 98% faster || With and without Stack || Cleane & Concise 📌📌 | abhi9Rai | 27 | 2,700 | decode string | 394 | 0.576 | Medium | 6,831 |
https://leetcode.com/problems/decode-string/discuss/1400105/98-faster-oror-With-and-without-Stack-oror-Cleane-and-Concise | class Solution:
def decodeString(self, s: str) -> str:
def dfs(s,p):
res = ""
i,num = p,0
while i<len(s):
asc = (ord(s[i])-48)
if 0<=asc<=9: # can also be written as if s[i].isdigit()
num=num*10+asc
elif s[i]=="[":
... | decode-string | 🐍 98% faster || With and without Stack || Cleane & Concise 📌📌 | abhi9Rai | 27 | 2,700 | decode string | 394 | 0.576 | Medium | 6,832 |
https://leetcode.com/problems/decode-string/discuss/1851228/Java-and-Python3-oror-Stack-oror-Clear-annotation | class Solution:
def decodeString(self, s):
# In python, List can be used as stack(by using pop()) and queue(by using pop(0))
result = []
for curr_char in s:
if curr_char == "]":
# Step2-1 : Find the (1)subStr and remove "[" in the stack
sub_str = [... | decode-string | Java and Python3 || Stack || Clear annotation | Sarah_Lene | 7 | 617 | decode string | 394 | 0.576 | Medium | 6,833 |
https://leetcode.com/problems/decode-string/discuss/714732/Python-solution-using-stacks.-O(n) | class Solution:
def decodeString(self, s: str) -> str:
# instantiate stacks to store the number and the string to repeat.
repeatStr = []
numRepeat = []
# initialize empty strings. One to store a multidigit number and other one to store the decoded string.
tempNum = ... | decode-string | Python solution using stacks. O(n) | darshan_22 | 6 | 490 | decode string | 394 | 0.576 | Medium | 6,834 |
https://leetcode.com/problems/decode-string/discuss/1638379/Very-Easy-Python-stack-O(n)-96-faster | class Solution:
def decodeString(self, s: str) -> str:
st = []
for c in s:
if c != ']':
st.append(c)
else:
# join the string inside the 1st balanced brackets
tmp = ""
while st and st[-1] != '[': ... | decode-string | Very Easy Python stack ; O(n) ; 96% faster | shankha117 | 4 | 253 | decode string | 394 | 0.576 | Medium | 6,835 |
https://leetcode.com/problems/decode-string/discuss/1167364/Concise-and-simple-recursive-solution | class Solution:
def decodeString(self, s: str) -> str:
def recurse(s, pos):
result = ""
i, num = pos, 0
while i < len(s):
c = s[i]
if c.isdigit():
num = num * 10 + in... | decode-string | Concise and simple recursive solution | swissified | 4 | 443 | decode string | 394 | 0.576 | Medium | 6,836 |
https://leetcode.com/problems/decode-string/discuss/2648343/Python-or-Easy-to-Understand-or-Stack | class Solution:
def decodeString(self, s: str) -> str:
'''
1.Use a stack and keep appending to the stack until you come across the first closing bracket(']')
2.When you come across the first closing bracket start popping until you encounter an opening bracket('[')),basically iterate unit the... | decode-string | Python | Easy to Understand | Stack | Ron99 | 2 | 245 | decode string | 394 | 0.576 | Medium | 6,837 |
https://leetcode.com/problems/decode-string/discuss/2386986/Python-solution-using-Recursion | class Solution:
def decodeString(self, s: str) -> str:
def helper(index, encodedStr):
k = 0
string = ""
# len(s) will be diffrent depend of subString
while index < len(encodedStr):
if encodedStr[index] == "[": # we found opening b... | decode-string | Python solution using Recursion | amalk5 | 2 | 192 | decode string | 394 | 0.576 | Medium | 6,838 |
https://leetcode.com/problems/decode-string/discuss/1464324/Python-Simple-Recursion | class Solution:
def decodeString(self, s: str) -> str:
if not any(map(str.isdigit, s)):
return s
inner = self.find(s)
back = s.index(']')
times, val = self.getNumber(s, inner)
string = s[inner+ 1: back]
s = s.replace(s[val:back+1],string*int(times))
... | decode-string | Python Simple Recursion | bubae | 2 | 436 | decode string | 394 | 0.576 | Medium | 6,839 |
https://leetcode.com/problems/decode-string/discuss/2525162/Python3-Solution-oror-Stack-oror-Easy-and-Understandable | class Solution:
def decodeString(self, s: str) -> str:
stk = []
for i in range(len(s)):
if s[i] != ']':
stk.append(s[i])
else:
strr = ''
while stk[-1] != '[':
strr = stk.pop() + strr
... | decode-string | Python3 Solution || Stack || Easy & Understandable | shashank_shashi | 1 | 82 | decode string | 394 | 0.576 | Medium | 6,840 |
https://leetcode.com/problems/decode-string/discuss/2348237/Python-or-Stack-or-Faster-than-80-or-Straight-forward | class Solution:
def decodeString(self, s: str) -> str:
i = 0
digitStart = []
leftBracket = []
while i < len(s):
if s[i].isdigit() and not len(digitStart) > len(leftBracket):
digitStart.append(i) # store index in stack
if s[i]... | decode-string | Python | Stack | Faster than 80% | Straight forward | pcdean2000 | 1 | 126 | decode string | 394 | 0.576 | Medium | 6,841 |
https://leetcode.com/problems/decode-string/discuss/2288079/Python3-solution-using-stack-faster-97 | class Solution:
def decodeString(self, s: str) -> str:
int_stack = []
str_stack = []
int_value = ""
for i in s:
if i.isdigit():
int_value+=i
else:
if i=="]":
k = ""
while len(str_stack) an... | decode-string | 📌 Python3 solution using stack faster 97% | Dark_wolf_jss | 1 | 81 | decode string | 394 | 0.576 | Medium | 6,842 |
https://leetcode.com/problems/decode-string/discuss/2183469/Python3-Runtime%3A-42ms-55.87-Memory%3A-13.9mb-20.28 | class Solution:
# Runtime: 42ms 55.87% Memory: 13.9mb 20.28%
def decodeString(self, string: str) -> str:
return self.solTwo(string)
def solOne(self, string):
stack = []
currentString = str()
currentNum = 0
for char in string:
if char.isd... | decode-string | Python3 Runtime: 42ms 55.87% Memory: 13.9mb 20.28% | arshergon | 1 | 166 | decode string | 394 | 0.576 | Medium | 6,843 |
https://leetcode.com/problems/decode-string/discuss/2010668/Python-solution-using-stack | class Solution:
def decodeString(self, s: str) -> str:
stack = []
for ch in s:
if ch == "]" and stack:
el = ""
while stack and not el.startswith("["):
el = stack.pop() + el
while stack and stack[-1].isdigit():
... | decode-string | Python solution using stack | n_inferno | 1 | 123 | decode string | 394 | 0.576 | Medium | 6,844 |
https://leetcode.com/problems/decode-string/discuss/1860736/Using-Python's-Magic-oror-Please-help-with-space-Complexity | class Solution:
def decodeString(self, s: str) -> str:
layer = {}
timesMap = {}
openCount = 0
idx = 0
while idx < len(s):
ch = s[idx]
if ch.isalpha():
layer[openCount] = layer.get(openCount, "") + ch
... | decode-string | Using Python's Magic || Please help with space Complexity | beginne__r | 1 | 84 | decode string | 394 | 0.576 | Medium | 6,845 |
https://leetcode.com/problems/decode-string/discuss/1776931/Easy-Understanding-Solution-with-Comments | class Solution:
def decodeString(self, s: str) -> str:
stack=[]
for i in s:
#if the character is not equal to closing bracket till then we will simply append the input
if i !="]":
stack.append(i)
else:
#now if it is closing bracket ... | decode-string | Easy Understanding Solution with Comments | dpatel1507 | 1 | 79 | decode string | 394 | 0.576 | Medium | 6,846 |
https://leetcode.com/problems/decode-string/discuss/1636383/Backward-pass-%3A-no-recursion-or-stacks | class Solution:
def decodeString(self, s: str) -> str:
for i in range(len(s) - 1, -1, -1):
if s[i].isdigit():
n = s[i]
k = i - 1
while k > - 1 and s[k].isdigit(): #Reading the full number
n = s[k] + n
k-... | decode-string | Backward pass : no recursion or stacks | Sima24 | 1 | 58 | decode string | 394 | 0.576 | Medium | 6,847 |
https://leetcode.com/problems/decode-string/discuss/825528/Python3-stack-O(N) | class Solution:
def decodeString(self, s: str) -> str:
stack = []
ans = num = ""
for c in s:
if c.isalpha(): ans += c
elif c.isdigit(): num += c
elif c == "[":
stack.append(num)
stack.append(ans)
ans = num ... | decode-string | [Python3] stack O(N) | ye15 | 1 | 102 | decode string | 394 | 0.576 | Medium | 6,848 |
https://leetcode.com/problems/decode-string/discuss/690861/simple-python-solution-using-stacks-(98.46) | class Solution:
def decodeString(self, s: str) -> str:
repeatStr = []
numRepeat = []
temp = ''
solution = ''
for char in s:
if char.isdigit():
temp += char
elif char == '[':
numRepeat.append(temp)
... | decode-string | simple python solution using stacks (98.46%) | darshan_22 | 1 | 178 | decode string | 394 | 0.576 | Medium | 6,849 |
https://leetcode.com/problems/decode-string/discuss/470592/Python3-simple-short-for()-loop | class Solution:
def decodeString(self, s: str) -> str:
stack,num,temp = [],"",""
for char in s:
if char == "[":
stack.append(temp),stack.append(num)
temp,num = "",""
elif char == "]":
count,prev = stack.pop(),stack.pop()
temp = prev + int(count)*temp
elif char.isdigit():
num += char
... | decode-string | Python3 simple short for() loop | jb07 | 1 | 100 | decode string | 394 | 0.576 | Medium | 6,850 |
https://leetcode.com/problems/decode-string/discuss/2846618/Python-solution-with-explanation-or-O(n) | class Solution:
def decodeString(self, s: str) -> str:
# finding if whole string is alphabetic or not with flag
flag = True
for letter in s:
if letter.isnumeric():
flag= False
break
# If whole string is alphabetic then return it
if(flag)... | decode-string | Python solution with explanation | O(n) | samart3010 | 0 | 5 | decode string | 394 | 0.576 | Medium | 6,851 |
https://leetcode.com/problems/decode-string/discuss/2846347/python3-solution-98.3-beats | class Solution:
def decodeString(self, s: str) -> str:
if s=='':
return s
if '[' not in s:
return s
i_o_b = s.rfind('[')
i_c_b= s.find(']',i_o_b)
a = int(s[i_o_b-1])
cnt = i_o_b - 2
try:
if type(int(s[i_o_b - 2])) ... | decode-string | python3 solution / 98.3 beats | Cosmodude | 0 | 3 | decode string | 394 | 0.576 | Medium | 6,852 |
https://leetcode.com/problems/decode-string/discuss/2846284/python3-solution-98.3-beats | class Solution:
def decodeString(self, s: str) -> str:
if s=='':
return s
if '[' not in s:
return s
i_o_b = s.rfind('[')
i_c_b= s.find(']',i_o_b)
a = int(s[i_o_b-1])
cnt = i_o_b - 2
try:
if type(int(s[i_o_b - 2])) ... | decode-string | python3 solution / 98.3 beats | Cosmodude | 0 | 2 | decode string | 394 | 0.576 | Medium | 6,853 |
https://leetcode.com/problems/decode-string/discuss/2828189/Decode-String-or-Python-Solution-or-Beats-97.14 | class Solution:
def decodeString(self, s: str) -> str:
stack = []
for i in s:
if i != ']':
stack.append(i)
else:
encodeString = ''
while stack and stack[-1] != '[':
encodeString = stack.pop() + encodeString
... | decode-string | Decode String | Python Solution | Beats 97.14% | nishanrahman1994 | 0 | 4 | decode string | 394 | 0.576 | Medium | 6,854 |
https://leetcode.com/problems/decode-string/discuss/2827501/Python3-solution | class Solution:
def decodeString(self, s: str) -> str:
stack = []
output = []
i = len(s) - 1
while i >= 0:
if len(stack) == 0 and s[i] != ']':
output = [s[i]] + output
i -= 1
continue
if s[i] == ']':
... | decode-string | Python3 solution | dmitrik | 0 | 5 | decode string | 394 | 0.576 | Medium | 6,855 |
https://leetcode.com/problems/decode-string/discuss/2824899/Python-easy-solution | class Solution:
def decodeString(self, s: str) -> str:
stack=[]
cur_level=[]
num=0
for char in s:
if char.isdigit():
num = num*10+int(char)
elif char.isalpha():
cur_level.append(char)
elif char == '[':
... | decode-string | Python easy solution | welin | 0 | 2 | decode string | 394 | 0.576 | Medium | 6,856 |
https://leetcode.com/problems/decode-string/discuss/2803780/Stack-oror-O(n)-oror-54ms-oror-Python3 | class Solution:
def decodeString(self, s: str) -> str:
stack = []
num = 0
res = ""
for st in s:
if st.isdigit():
num = num*10 + int(st)
elif st == '[':
stack.append(res)
stack.append(num)
res = ""... | decode-string | Stack || O(n) || 54ms || Python3 | spi-der_3ks | 0 | 5 | decode string | 394 | 0.576 | Medium | 6,857 |
https://leetcode.com/problems/decode-string/discuss/2684869/python-solution-stack-method | class Solution:
def decodeString(self, s: str) -> str:
p = ""
n = 0
stack = []
for i in s:
if i.isdigit():
n = n * 10 + int(i)
elif i == '[':
stack.append((p,n))
p = ''
n = 0
elif i ==... | decode-string | python solution stack method | vivekraj185 | 0 | 89 | decode string | 394 | 0.576 | Medium | 6,858 |
https://leetcode.com/problems/decode-string/discuss/2660470/python-stack-solution | class Solution:
def decodeString(self, s: str) -> str:
stack = []
i = 0
N = len(s)
while i < N:
if s[i].isdigit():
d = ""
while s[i].isdigit():
d += s[i]
i += 1
stack.append(int(d))
... | decode-string | python stack solution | danielturato | 0 | 11 | decode string | 394 | 0.576 | Medium | 6,859 |
https://leetcode.com/problems/decode-string/discuss/2650081/Easy-to-understand-Python-Solution | class Solution:
def decodeString(self, s: str) -> str:
res=""
b1,b2=0,0
i=len(s)-1
while i>=0:
if s[i].isnumeric():
b2=i
while s[i].isnumeric():
i-=1
b1=i+1
n=int(s[b1:b2+1])
... | decode-string | Easy to understand Python Solution | afrinmahammad | 0 | 17 | decode string | 394 | 0.576 | Medium | 6,860 |
https://leetcode.com/problems/decode-string/discuss/2490808/Easy-solution-using-RE-(regular-expression)-Python-3-faster-than-80 | class Solution:
def decodeString(self, s: str) -> str:
while True:
res = re.search('\d*\[[^[^\]]*?\]', s)
if not res:
return s
start, end = res.span()
string = s[start:end]
number, right = string.split('[')
right = right... | decode-string | Easy solution using RE (regular expression) Python 3 faster than 80% | JiaxuLi | 0 | 31 | decode string | 394 | 0.576 | Medium | 6,861 |
https://leetcode.com/problems/decode-string/discuss/2459877/Python3-Solution-29-ms-faster-than-95.43-of-Python3-online-submissions-for-Decode-String. | class Solution:
def decodeString(self, s: str) -> str:
st = ''
i = 0
while i < len(s):
if not s[i].isdigit():
st = st + s[i]
else:
findB = s.index('[', i)
d = int(s[i : findB])
j = findB + 1
... | decode-string | [Python3] Solution 29 ms, faster than 95.43% of Python3 online submissions for Decode String. | WhiteBeardPirate | 0 | 80 | decode string | 394 | 0.576 | Medium | 6,862 |
https://leetcode.com/problems/decode-string/discuss/2430278/Python3-or-Solved-Using-Recursion-%2B-Stack | class Solution:
def decodeString(self, s: str) -> str:
#base case: single character that's not a number!
if(len(s) == 1 and s.isdigit() == False and s[0] != '[' and s[0] != ']'):
return s
#otherwise, we need to intialize the ans variable which we will return at the end!
... | decode-string | Python3 | Solved Using Recursion + Stack | JOON1234 | 0 | 69 | decode string | 394 | 0.576 | Medium | 6,863 |
https://leetcode.com/problems/decode-string/discuss/2291011/Python3-Solution-with-using-stack | class Solution:
def decodeString(self, s: str) -> str:
stack = ['']
num = 0
for ch in s:
if ch.isdigit():
num = num * 10 + int(ch)
elif ch == '[':
stack.append(num)
num = 0
stack.append("")
... | decode-string | [Python3] Solution with using stack | maosipov11 | 0 | 53 | decode string | 394 | 0.576 | Medium | 6,864 |
https://leetcode.com/problems/decode-string/discuss/2223844/Python-Using-Stack-O(n2)-solution | class Solution:
def decodeString(self, s: str) -> str:
def solve(s):
ans = ""
stack = deque()
for i in range(len(s)):
if s[i] != ']':
stack.append(s[i])
if s[i] == ']':
temp = ""
... | decode-string | Python Using Stack O(n^2) solution | Abhi_009 | 0 | 36 | decode string | 394 | 0.576 | Medium | 6,865 |
https://leetcode.com/problems/decode-string/discuss/2223464/Stack-Approach-oror-Clean-Code | class Solution:
def decodeString(self, s: str) -> str:
stack = []
n = len(s)
for i in range(n):
if s[i] != "]":
stack.append(s[i])
else:
substring = ""
while stack[-1] != "[":
substring = sta... | decode-string | Stack Approach || Clean Code | Vaibhav7860 | 0 | 96 | decode string | 394 | 0.576 | Medium | 6,866 |
https://leetcode.com/problems/decode-string/discuss/2144759/Python-recursive | class Solution:
def decodeString(self, s: str) -> str:
def decode():
result = ""
while self.i < len(s) and s[self.i] != ']':
if s[self.i].isdigit():
idx = s.index('[', self.i)
digit = int(s[self.i:idx])
self.... | decode-string | Python, recursive | blue_sky5 | 0 | 93 | decode string | 394 | 0.576 | Medium | 6,867 |
https://leetcode.com/problems/decode-string/discuss/2076218/Python-Stack-Solution | class Solution:
def decodeString(self, s: str) -> str:
stack = []
for _char in s:
if _char != ']':
stack.append(_char)
else:
decoded_str = ''
while stack[-1] != '[':
stored_char = stack.pop(-1)
... | decode-string | Python Stack Solution | ankitkools | 0 | 164 | decode string | 394 | 0.576 | Medium | 6,868 |
https://leetcode.com/problems/decode-string/discuss/1967225/Easy-and-Fast-Solution-in-Python-using-Stack-T.C-greaterO(n) | class Solution:
def decodeString(self, s: str) -> str:
stack=[]
for i in range(len(s)):
if s[i] !="]":
stack.append(s[i])
else:
substring=""
while stack[-1]!="[":
substring=stack.pop()+substring
... | decode-string | Easy and Fast Solution in Python using Stack T.C->O(n) | karansinghsnp | 0 | 61 | decode string | 394 | 0.576 | Medium | 6,869 |
https://leetcode.com/problems/decode-string/discuss/1867917/Python-Recursive-Solution-(beats-99.78-) | class Solution:
def decodeString(self, s: str, start=0) -> str:
self.start = start
result = []
number_str = []
while self.start < len(s) and s[self.start] != ']':
if s[self.start].isalpha():
result.append(s[self.start])
elif s[self.sta... | decode-string | ✅ Python Recursive Solution (beats 99.78 %) | AntonBelski | 0 | 406 | decode string | 394 | 0.576 | Medium | 6,870 |
https://leetcode.com/problems/decode-string/discuss/1858574/Python-(non-recursive-no-stack-beats-99.79) | class Solution:
def decodeString(self, s: str) -> str:
# start from the back
i = len(s) - 1
while i >= 0:
# check to see if it is a number and capture the entire number if it is (chr 48-57 == 0-9)
n = i
while 47 < ord(s[i]) < 58:
... | decode-string | Python (non recursive, no stack, beats 99.79%) | esun74 | 0 | 66 | decode string | 394 | 0.576 | Medium | 6,871 |
https://leetcode.com/problems/decode-string/discuss/1843162/Python-l-Simple-Pointers | class Solution:
def decodeString(self, s: str) -> str:
i = 0
j = len(s) - 1
while '[' in s and ']' in s:
while '[' in s[i+1:j]: i += 1
while ']' in s[i+1:j]: j -= 1
k = i - 1
nb = ''
while s[k].isdigit():
nb = s[k] + nb
k -= 1
decode = int(nb)*s[i+1:j]
s = s[:k+1] + decode + s[... | decode-string | Python l Simple Pointers | morpheusdurden | 0 | 93 | decode string | 394 | 0.576 | Medium | 6,872 |
https://leetcode.com/problems/decode-string/discuss/1637304/Python3-Use-stack-and-keep-track-of-state | class Solution:
def decodeString(self, s: str) -> str:
res = []
temp = ""
num = ""
content = []
for i, c in enumerate(s):
if c.isnumeric():
if temp:
res.append(temp)
temp = ""
num+=c
... | decode-string | [Python3] Use stack and keep track of state | Rainyforest | 0 | 15 | decode string | 394 | 0.576 | Medium | 6,873 |
https://leetcode.com/problems/decode-string/discuss/1636259/Python-simple-solutionor-95.62 | class Solution:
def decodeString(self, s: str) -> str:
# pay attention to numbers larger than 9
if s.isalpha():
return s
n = len(s)
count = left = 0
num = -1
ret = ''
for i in range(n):
if count==0 and s[i].isalpha():
re... | decode-string | Python simple solution| 95.62% | 1579901970cg | 0 | 54 | decode string | 394 | 0.576 | Medium | 6,874 |
https://leetcode.com/problems/decode-string/discuss/1635930/Python-99-fast-solution | class Solution:
def decodeString(self, s: str) -> str:
ans = ""
for i in s:
# print(ans)
if i==']':
pos = len(ans)-1
temp = ""
while ans[pos]!='[':
temp = ans[pos] +temp
pos-=1
... | decode-string | Python 99% fast solution | RedHeadphone | 0 | 250 | decode string | 394 | 0.576 | Medium | 6,875 |
https://leetcode.com/problems/decode-string/discuss/1635676/Recursion-with-stack-approachoror24ms-faster-than-95.62oror14.3MB-less-than-52.98ororPython3 | class Solution:
def decodeString(self, s: str) -> str:
def helper(sub):
res = '' # empty new string initailaized
i = 0 # every time we get a valid new substring, we need to traverse through all of it
while i < len(sub)... | decode-string | Recursion with stack approach||24ms faster than 95.62%||14.3MB less than 52.98%||Python3 | nandhakiran366 | 0 | 32 | decode string | 394 | 0.576 | Medium | 6,876 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/1721267/Faster-than-97.6.-Recursion | class Solution:
def rec(self, s, k):
c = Counter(s)
if pattern := "|".join(filter(lambda x: c[x] < k, c)):
if arr := list(filter(lambda x: len(x) >= k, re.split(pattern, s))):
return max(map(lambda x: self.rec(x, k), arr))
return 0
... | longest-substring-with-at-least-k-repeating-characters | Faster than 97.6%. Recursion | mygurbanov | 3 | 244 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,877 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/1041531/Python-Sliding-Window-Solution | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
#sliding window and hashmap O(n) or Divide and conquer(O(n*n))
n=len(s)
ans=0
freq= Counter(s)
max_nums=len(freq)
for num in range(1,max_nums+1):
counter=defaultdict(int)
l... | longest-substring-with-at-least-k-repeating-characters | Python Sliding Window Solution | ShivamBunge | 3 | 1,000 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,878 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/704604/Python-3Longest-Substring-with-Atleast-K-repeating-characters.-Beats-85. | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
if len(s)==0:
return 0
cnt = collections.Counter(s)
for i in cnt:
if cnt[i] < k:
# print(s.split(i))
return max(self.longestSubstring(p,k) for... | longest-substring-with-at-least-k-repeating-characters | [Python 3]Longest Substring with Atleast K repeating characters. Beats 85%. | tilak_ | 3 | 696 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,879 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/2549197/easy-sliding-window-approach | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
# number of unique characters available
max_chars = len(set(s))
n = len(s)
ans = 0
# for all char from 1 to max_chars
for available_char in range(1,max_chars+1):
h = {}
i = j = 0
# simple sliding window approach
while(j < ... | longest-substring-with-at-least-k-repeating-characters | easy sliding window approach | jagdishpawar8105 | 2 | 267 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,880 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/825847/Python3-divide-and-conquer | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
if not s: return 0 # edge case
freq = {} # frequency table
for c in s: freq[c] = 1 + freq.get(c, 0)
if min(freq.values()) < k:
m = min(freq, key=freq.get)
return max(se... | longest-substring-with-at-least-k-repeating-characters | [Python3] divide & conquer | ye15 | 2 | 279 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,881 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/1847012/Python-easy-to-read-and-understand-or-recursion | class Solution:
def solve(self, s, k):
if len(s) < k:
return 0
for i in set(s):
if s.count(i) < k:
split = s.split(i)
ans = 0
for substring in split:
ans = max(ans, self.solve(substring, k))
r... | longest-substring-with-at-least-k-repeating-characters | Python easy to read and understand | recursion | sanial2001 | 1 | 238 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,882 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/2781833/python | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
ret = 0
n = len(s)
for t in range(len(set(s)) + 1):
l, r = 0, 0
cnt = [0] * 26
tot, less = 0, 0
while r < n:
cnt[ord(s[r]) - 97] += 1
if cnt[ord(... | longest-substring-with-at-least-k-repeating-characters | python | xy01 | 0 | 12 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,883 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/2781833/python | class Solution:
def longestSubstring(self, s, k):
def dfs(s, l , r, k):
cnt = [0] * 26
for i in range(l, r + 1):
cnt[ord(s[i]) - 97] += 1
split = 0
for i in range(26):
if cnt[i] > 0 and cnt[i] < k:
s... | longest-substring-with-at-least-k-repeating-characters | python | xy01 | 0 | 12 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,884 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/2733987/Faster-than-98-Easy-and-Small-Solution | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
if k > len(s):
return 0
for letter in set(s):
if s.count(letter) < k:
temp = s.split(letter)
return max(self.longestSubstring(division, k) for division in temp)
return l... | longest-substring-with-at-least-k-repeating-characters | Faster than 98%, Easy and Small Solution | user6770yv | 0 | 12 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,885 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/2722139/python-easy-solution-faster | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
if(len(s)==0):
return 0
cnt=Counter(s)
for i,j in cnt.items():
if(j<k):
return max(self.longestSubstring(p,k) for p in s.split(i))
return len(s) | longest-substring-with-at-least-k-repeating-characters | python easy solution faster | Raghunath_Reddy | 0 | 17 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,886 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/2657162/Python3-Solution-Divide-and-Conquer-Skip-repeat-charaters-when-Divide | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
from collections import Counter
s_counter = Counter(s)
print('initial Counter', s_counter)
for i in range(len(s)):
if s_counter[s[i]]<k:
print('i position', i)
print('s[i... | longest-substring-with-at-least-k-repeating-characters | Python3 Solution - Divide and Conquer - Skip repeat charaters when Divide | ben_wei | 0 | 8 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,887 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/2433742/Divide-an-Conquer-oror-Recursion-oror-Python3 | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
def divideConquer(string):
left = 0
right = 0
counter = Counter(string)
for index, char in enumerate(string):
if index == len(string) - 1 and counter[char]>=k:
... | longest-substring-with-at-least-k-repeating-characters | Divide an Conquer || Recursion || Python3 | Sefinehtesfa34 | 0 | 25 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,888 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/2407423/Python-Solution-or-Recursive-Solution-or-90-Faster-or-Divide-and-Conquer | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
# consider this as base case
if len(s) < k:
return 0
# get character with lowest frequency
minFChar, minF = collections.Counter(s).most_common()[-1]
# is minimum frequency valid?
... | longest-substring-with-at-least-k-repeating-characters | Python Solution | Recursive Solution | 90% Faster | Divide and Conquer | Gautam_ProMax | 0 | 83 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,889 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/1869291/Python3-Sliding-Window-with-distinct-character-limit | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
max_chars = len(Counter(s))
ans = 0
for size in range(1, max_chars+1):
i = 0
mp = Counter()
for j in range(len(s)):
# limit sliding window by number of distinct characters
... | longest-substring-with-at-least-k-repeating-characters | Python3 Sliding Window with distinct character limit | zhuzhengyuan824 | 0 | 278 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,890 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/1709125/Python-or-Hashmap-or-Recursion | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
ans=0
def dfs(tmp):
nonlocal ans
for x in tmp:
if x:
ct=Counter(x)
if ct.most_common()[-1][-1]>=k:#Case like 'ababab or aaabbb' Means all the characters ... | longest-substring-with-at-least-k-repeating-characters | Python | Hashmap | Recursion | heckt27 | 0 | 108 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,891 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/1601863/Recursive-or-8-lines-codes | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
if not s:
return 0
if len(s) < k:
return 0
for i in set(s):
if s.count(i)< k:
return max([self.longestSubstring(substr, k) for substr in s.split(i)])
ret... | longest-substring-with-at-least-k-repeating-characters | Recursive | 8 lines codes | zixin123 | 0 | 142 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,892 |
https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/503083/Simple!-By-counting-consecutive-repeating-chars | class Solution:
def longestSubstring(self, s: str, k: int) -> int:
c_counts = []
pc = None
n = 0
for c in s:
if pc is None:
pc = s
n = 1
else:
if pc == c:
n += 1
else:
... | longest-substring-with-at-least-k-repeating-characters | Simple! By counting consecutive repeating chars | user1409N | 0 | 219 | longest substring with at least k repeating characters | 395 | 0.448 | Medium | 6,893 |
https://leetcode.com/problems/rotate-function/discuss/857056/Python-3-(Py3.8)-or-Math-O(n)-or-Explanation | class Solution:
def maxRotateFunction(self, A: List[int]) -> int:
s, n = sum(A), len(A)
cur_sum = sum([i*j for i, j in enumerate(A)])
ans = cur_sum
for i in range(n): ans = max(ans, cur_sum := cur_sum + s-A[n-1-i]*n)
return ans | rotate-function | Python 3 (Py3.8) | Math, O(n) | Explanation | idontknoooo | 6 | 588 | rotate function | 396 | 0.404 | Medium | 6,894 |
https://leetcode.com/problems/rotate-function/discuss/1913574/Python-easy-understanding-solution-with-comment | class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
s, n = sum(nums), len(nums)
rotate_sum = 0
for i in range(n):
rotate_sum += nums[i] * i # ex. [0, 1, 2, 3] --> 0*0 + 1*1 + 2*2 + 3*3
res = rotate_sum
for i i... | rotate-function | Python easy - understanding solution with comment | byroncharly3 | 1 | 104 | rotate function | 396 | 0.404 | Medium | 6,895 |
https://leetcode.com/problems/rotate-function/discuss/825648/Python3-O(N)-time | class Solution:
def maxRotateFunction(self, A: List[int]) -> int:
ans = val = sum(i*x for i, x in enumerate(A))
ss = sum(A)
for x in reversed(A):
val += ss - len(A)*x
ans = max(ans, val)
return ans | rotate-function | [Python3] O(N) time | ye15 | 1 | 189 | rotate function | 396 | 0.404 | Medium | 6,896 |
https://leetcode.com/problems/rotate-function/discuss/2832843/Beat-98-Eliminate-redundant-re-calculation-DP-or-memo-python-simple-solution | class Solution:
def maxRotateFunction(self, nums: List[int]) -> int:
# 25 - 6 * (N - 1) + sum(nums) - 6
# cur - last_element * N + sum(nums)
# eliminate overlapping sub-problem, no need re-calculation
# only keep track last element
N = len(nums)
total = sum(nums)
... | rotate-function | Beat 98% / Eliminate redundant re-calculation / DP or memo / python simple solution | Lara_Craft | 0 | 2 | rotate function | 396 | 0.404 | Medium | 6,897 |
https://leetcode.com/problems/rotate-function/discuss/2800420/Python-(Simple-Dynamic-Programming) | class Solution:
def maxRotateFunction(self, nums):
n, total = len(nums), sum(nums)
dp = [0]*n
dp[0] = sum([i*j for i,j in enumerate(nums)])
for i in range(1,n):
dp[i] = dp[i-1] + (total - nums[n-i]*n)
return max(dp) | rotate-function | Python (Simple Dynamic Programming) | rnotappl | 0 | 3 | rotate function | 396 | 0.404 | Medium | 6,898 |
https://leetcode.com/problems/rotate-function/discuss/2705033/Python3-Solution-or-O(n) | class Solution:
def maxRotateFunction(self, A):
csum, n = sum(A), len(A)
ans = cur = sum(i * A[i] for i in range(n))
for i in range(n - 1):
cur += csum - A[n - i - 1] * n
ans = max(ans, cur)
return ans | rotate-function | ✔ Python3 Solution | O(n) | satyam2001 | 0 | 7 | rotate function | 396 | 0.404 | Medium | 6,899 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.