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/maximum-ice-cream-bars/discuss/1163987/Python-Solution | class Solution:
def maxIceCream(self, costs: List[int], coins: int) -> int:
costs.sort()
res = 0
for cost in costs:
if coins >= cost :
coins -= cost
res += 1
return res | maximum-ice-cream-bars | Python Solution | SaSha59 | 0 | 42 | maximum ice cream bars | 1,833 | 0.656 | Medium | 26,200 |
https://leetcode.com/problems/maximum-ice-cream-bars/discuss/1164123/Python3-Very-Simple-Solution | class Solution:
def maxIceCream(self, costs: List[int], coins: int) -> int:
if min(costs) > coins:
return 0
if sum(costs) < coins:
return len(costs)
costs.sort()
count = 0
for i in costs:
if coins < i:
break
coun... | maximum-ice-cream-bars | Python3 Very Simple Solution | VijayantShri | -1 | 46 | maximum ice cream bars | 1,833 | 0.656 | Medium | 26,201 |
https://leetcode.com/problems/single-threaded-cpu/discuss/2004757/Python-or-Priority-Queue | class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
dic=defaultdict(list)
for i in range(len(tasks)):
dic[tasks[i][0]].append((tasks[i][1],i))
ans=[]
keys=sorted(dic.keys())
while keys:
k=keys.po... | single-threaded-cpu | Python | Priority Queue | heckt27 | 1 | 83 | single threaded cpu | 1,834 | 0.42 | Medium | 26,202 |
https://leetcode.com/problems/single-threaded-cpu/discuss/1817100/Clean-Python-Two-Heaps | class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
heap = []
available = []
for i, (e, p) in enumerate(tasks):
heappush(heap, (e, p, i))
e, p, i = heappop(heap)
last = e + p
res = [i]
while heap or available: ... | single-threaded-cpu | Clean Python - Two Heaps | r_vaghefi | 1 | 156 | single threaded cpu | 1,834 | 0.42 | Medium | 26,203 |
https://leetcode.com/problems/single-threaded-cpu/discuss/2806756/Python-min-heap | class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
minHeap = []
res = []
for i in range(len(tasks)):
tasks[i] += [i]
tasks.sort(key=lambda x: x[0])
t = tasks[0][0]
while minHeap or tasks:
while tasks and t >= tasks[0][0... | single-threaded-cpu | Python min heap | zananpech9 | 0 | 2 | single threaded cpu | 1,834 | 0.42 | Medium | 26,204 |
https://leetcode.com/problems/single-threaded-cpu/discuss/1815409/Solution-using-heapq-in-Python-3 | class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
def cpu_scheduler():
import heapq
nonlocal tasks
tasks = [(*task, i) for i, task in enumerate(tasks)]
tasks.sort()
pq = []
clk = tasks[0][0]
i... | single-threaded-cpu | Solution using heapq in Python 3 | mousun224 | 0 | 120 | single threaded cpu | 1,834 | 0.42 | Medium | 26,205 |
https://leetcode.com/problems/single-threaded-cpu/discuss/1166935/Python3-priority-queue | class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
ans = []
pq = [] # min-heap
t = 0 # end of prev task
tasks.append([inf, inf])
for (enq, prc), i in sorted(zip(tasks, range(len(tasks)))): # adding a sentinel
while pq and t < enq: ... | single-threaded-cpu | [Python3] priority queue | ye15 | 0 | 80 | single threaded cpu | 1,834 | 0.42 | Medium | 26,206 |
https://leetcode.com/problems/single-threaded-cpu/discuss/1166183/Python3-Heap-O(N-log-N)-With-comments | class Solution:
class Task:
def __init__(self,index,start,time):
self.index = index
self.start = start
self.time = time
def __lt__(self,other):
#compare by duration and index
if self.time != other.time:
return self.... | single-threaded-cpu | Python3 Heap O(N log N) With comments | necilAlbayrak | 0 | 129 | single threaded cpu | 1,834 | 0.42 | Medium | 26,207 |
https://leetcode.com/problems/single-threaded-cpu/discuss/1165019/Python-3-Minimum-Heap-(1872-ms) | class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
# sort by start time, process time and index
t = [(a, b, i) for i, (a, b) in enumerate(tasks)]
t.sort()
t = deque(t)
# set start time as first task end time
start = t[0][0] + t[0][1]
an... | single-threaded-cpu | [Python 3] Minimum Heap (1872 ms) | chestnut890123 | 0 | 74 | single threaded cpu | 1,834 | 0.42 | Medium | 26,208 |
https://leetcode.com/problems/single-threaded-cpu/discuss/1164818/python3-Solution-using-sort-and-heapq-for-reference. | class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
idxlist = list(zip(tasks, range(len(tasks))))
idxlist.sort(key=lambda x: (x[0][0], x[1]))
idxlist = list(map(lambda x: (x[0][0], x[1], x[0][1]), idxlist))
heapq.heapify(idxlist)
... | single-threaded-cpu | [python3] Solution using sort and heapq for reference. | vadhri_venkat | 0 | 66 | single threaded cpu | 1,834 | 0.42 | Medium | 26,209 |
https://leetcode.com/problems/single-threaded-cpu/discuss/1164505/Python3-heapq | class Solution:
def getOrder(self, tasks: List[List[int]]) -> List[int]:
for i in range(len(tasks)):
tasks[i] = [tasks[i][0],tasks[i][1],i] #add index into tasks array
tasks.sort(key = lambda x: (x[0],x[1])) #sort task according to arrival time and burst time
cpu_timestamp = ... | single-threaded-cpu | Python3 heapq | deleted_user | 0 | 75 | single threaded cpu | 1,834 | 0.42 | Medium | 26,210 |
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/2724403/Simple-python-code-with-explanation | class Solution:
#example 1
#result =[(1&6)^(1&5)^(2&6)^(2&5)^(3&6)^(3&5)]
\ / \ / \ /
# (1&(6^5)) ^ (2&(6^5)) ^ (3&(6^5))
\ | /
\ | /
... | find-xor-sum-of-all-pairs-bitwise-and | Simple python code with explanation | thomanani | 1 | 23 | find xor sum of all pairs bitwise and | 1,835 | 0.601 | Hard | 26,211 |
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1164299/Python3-solution-with-comments-100-memory-efficient | class Solution:
def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:
'''
1. According to the hints, XORSum = (XORSum of arr1) bitwise AND (XORSum of arr2)
2. Calculate the XOR Sums of arr1 and arr2 separately and store them in separate variables
3. Perform bitwise AND on tho... | find-xor-sum-of-all-pairs-bitwise-and | Python3 solution with comments, 100% memory efficient | bPapan | 1 | 76 | find xor sum of all pairs bitwise and | 1,835 | 0.601 | Hard | 26,212 |
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/2413037/92-faster-95-memory-python-3-w-comments | class Solution:
def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:
# GET MIN LOOP LENGTH
length = min(len(arr1), len(arr2))
# Break into 2 sets
a1 = 0
b1 = 0
# SHORT CUT IF THEY ARE THE SAME LENGTH
if len(arr1) == len(arr2... | find-xor-sum-of-all-pairs-bitwise-and | 92% faster / 95% memory python 3 w/ comments | cengleby86 | 0 | 36 | find xor sum of all pairs bitwise and | 1,835 | 0.601 | Hard | 26,213 |
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1565126/Python-Easy-Solution-or-Using-XOR | class Solution:
def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:
ele1 = arr1[0]
ele2 = arr2[0]
for i in range(1, len(arr1)):
ele1 = ele1 ^ arr1[i]
for j in range(1, len(arr2)):
ele2 = ele2 ^ arr2[j]
return ele1 & ele2 | find-xor-sum-of-all-pairs-bitwise-and | Python Easy Solution | Using XOR | leet_satyam | 0 | 85 | find xor sum of all pairs bitwise and | 1,835 | 0.601 | Hard | 26,214 |
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1490551/Python3-or-O(32*(m%2Bn)) | class Solution:
def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:
ans = 0
for i in range(32):
arr1_set_count = 0
arr2_set_count = 0
for x in arr1:
if x&(1<<i) != 0:
arr1_set_count ^= 1
... | find-xor-sum-of-all-pairs-bitwise-and | Python3 | O(32*(m+n)) | Sanjaychandak95 | 0 | 34 | find xor sum of all pairs bitwise and | 1,835 | 0.601 | Hard | 26,215 |
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1278432/Python3-Easiest | class Solution:
def getXORSum(self, a1: List[int], a2: List[int]) -> int:
ans1, ans2 = 0, 0
for i in range(len(a1)):
ans1 ^= a1[i]
for i in range(len(a2)):
ans2 ^= a2[i]
return ans1&ans2 | find-xor-sum-of-all-pairs-bitwise-and | Python3 Easiest | tglukhikh | 0 | 77 | find xor sum of all pairs bitwise and | 1,835 | 0.601 | Hard | 26,216 |
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1166939/Python3-1-line | class Solution:
def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:
return reduce(xor, arr1) & reduce(xor, arr2) | find-xor-sum-of-all-pairs-bitwise-and | [Python3] 1-line | ye15 | 0 | 42 | find xor sum of all pairs bitwise and | 1,835 | 0.601 | Hard | 26,217 |
https://leetcode.com/problems/find-xor-sum-of-all-pairs-bitwise-and/discuss/1164173/Python-straight-forward-solution | class Solution:
def getXORSum(self, arr1: List[int], arr2: List[int]) -> int:
"""
(a^b) & (d^e^f) = a&d ^ a&e ^ a&f ^ b&d ^ b&e ^ b&f
"""
N1 = len(arr1)
N2 = len(arr2)
tmp1 = 0
tmp2 = 0
for i in range(max(N1, N2)):
... | find-xor-sum-of-all-pairs-bitwise-and | Python straight forward solution | AlbertWang | 0 | 54 | find xor sum of all pairs bitwise and | 1,835 | 0.601 | Hard | 26,218 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1175067/Python3-self-explained | class Solution:
def sumBase(self, n: int, k: int) -> int:
ans = 0
while n:
n, x = divmod(n, k)
ans += x
return ans | sum-of-digits-in-base-k | [Python3] self-explained | ye15 | 12 | 1,200 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,219 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1175132/Python3-easy-solution | class Solution:
def sumBase(self, n: int, k: int) -> int:
output_sum = 0
while (n > 0) :
rem = n % k
output_sum = output_sum + rem
n = int(n / k)
return output_sum | sum-of-digits-in-base-k | {Python3} easy solution | AbhishekSingh212 | 5 | 427 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,220 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1845509/3-Lines-Python-Solution-oror-95-Faster-oror-Memory-less-than-75 | class Solution:
def sumBase(self, n: int, k: int) -> int:
ans=0
while n>0: ans+=n%k ; n//=k
return ans | sum-of-digits-in-base-k | 3-Lines Python Solution || 95% Faster || Memory less than 75% | Taha-C | 3 | 118 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,221 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1845509/3-Lines-Python-Solution-oror-95-Faster-oror-Memory-less-than-75 | class Solution:
def sumBase(self, n: int, k: int) -> int:
return (x:=lambda y: 0 if not y else y%k + x(y//k))(n) | sum-of-digits-in-base-k | 3-Lines Python Solution || 95% Faster || Memory less than 75% | Taha-C | 3 | 118 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,222 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1175073/PythonPython3-Solution | class Solution:
def sumBase(self, n: int, k: int) -> int:
cnt = 0
while n:
cnt += (n % k)
n //= k
print(cnt)
return cnt | sum-of-digits-in-base-k | Python/Python3 Solution | prasanthksp1009 | 3 | 212 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,223 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2613822/Python-Divmod-Solution | class Solution:
def sumBase(self, n: int, k: int) -> int:
result = 0
# make repeated divmods to get the digits and
# the leftover number
while n:
n, res = divmod(n, k)
result += res
return result | sum-of-digits-in-base-k | [Python] - Divmod Solution | Lucew | 2 | 31 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,224 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2287491/python-solution-fastest-and-efficient | class Solution:
def sumBase(self, n: int, k: int) -> int:
x=[]
while n!=0:
x.append(n%k)
n=n//k
return sum(x) | sum-of-digits-in-base-k | python solution fastest and efficient | yagnic40 | 2 | 42 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,225 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2317805/Easy-solution-oror-PYTHON | ```class Solution:
def sumBase(self, n: int, k: int) -> int:
stri = ""
while True:
if n < k:
break
div = int(n // k)
stri += str(n % k)
n = div
stri += str(n)
stri = stri[::-1]
lst = [int(x) for x in stri]
... | sum-of-digits-in-base-k | Easy solution || PYTHON | Jonny69 | 1 | 54 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,226 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2119575/5-lines-of-Python-Code-with-explanation | class Solution:
def sumBase(self, n: int, k: int) -> int:
sum1 = 0
while n:
sum1 += n%k # It gives the remainder and also add in each step to the variable sum1
n //= k
return sum1 | sum-of-digits-in-base-k | 5 lines of Python Code with explanation | prernaarora221 | 1 | 73 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,227 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2786542/Python-solution | class Solution:
def sumBase(self, n: int, k: int) -> int:
remainders = []
if (n // k) < k:
remainders.append(n % k)
remainders.append(n//k)
else:
while (n // k) >= 1:
remainders.append(n % k)
if (n // k) < k:
... | sum-of-digits-in-base-k | Python solution | samanehghafouri | 0 | 1 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,228 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2717358/Python-99.65faster-with-94less-memory | class Solution:
def sumBase(self, n: int, k: int) -> int:
ans = 0
while(1):
s = n%k
n = n//k
ans+=s
if(n<k):
ans+=n
break
return ans | sum-of-digits-in-base-k | Python 99.65%faster with 94%less memory | morrismoppp | 0 | 6 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,229 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2598952/python3-recursion-45ms | class Solution:
def sumBase(self, n: int, k: int) -> int:
if n == 0:
return 0
return (n % k) + self.sumBase(n // k, k) | sum-of-digits-in-base-k | python3 recursion 45ms | ranv1r | 0 | 4 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,230 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/2442380/Python-for-beginners | class Solution:
def sumBase(self, n: int, k: int) -> int:
#Runtime: 34ms
if(n<k): return n
if(n==k): return 1
lis=[]
while n>=k:
remainder=n%k
lis.append(remainder)
n=n//k
lis.append(n) #last value when n<k appending
return ... | sum-of-digits-in-base-k | Python for beginners | mehtay037 | 0 | 23 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,231 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1592353/Simple-Recursive-Solution | class Solution:
def sumBase(self, n: int, k: int) -> int:
if n <= 0:
return 0
return n % k + self.sumBase(n // k, k) | sum-of-digits-in-base-k | Simple Recursive Solution | pmooreh | 0 | 36 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,232 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1220829/Python-Easy-solution | class Solution:
def sumBase(self, n: int, k: int) -> int:
ans = 0
while n>=k:
ans = ans + n%k
n = n//k
ans = ans+n
return ans | sum-of-digits-in-base-k | [Python] Easy solution | arkumari2000 | 0 | 182 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,233 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1200393/Python3-simple-solution | class Solution:
def sumBase(self, n: int, k: int) -> int:
sum = 0
while n != 0:
sum += n%k
n = n//k
return sum | sum-of-digits-in-base-k | Python3 simple solution | EklavyaJoshi | 0 | 71 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,234 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1180105/Python-3-A-Normal-Mathematical-Solution | class Solution:
def sumBase(self, n: int, k: int) -> int:
fin = 0
while n>=k:
fin+=n%k
n=n//k
fin+=n
return fin | sum-of-digits-in-base-k | [Python 3] A Normal Mathematical Solution | vamsi81523 | 0 | 88 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,235 |
https://leetcode.com/problems/sum-of-digits-in-base-k/discuss/1175602/python-sol-faster-than-100-less-memory-than-100 | class Solution:
def sumBase(self, num: int, base: int) -> int:
tot = 0
base_num = ""
while num>0:
dig = int(num%base)
if dig<10:
base_num += str(dig)
else:
base_num += chr(ord('A')+dig-10)
... | sum-of-digits-in-base-k | python sol faster than 100% , less memory than 100% | elayan | 0 | 61 | sum of digits in base k | 1,837 | 0.768 | Easy | 26,236 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1179374/Python-3-Sliding-Window-Explanation-with-Code | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
n = len(nums)
sum_s_w = nums[0]
fin = 1
i=0
for j in range(1,n):
sum_s_w+=nums[j]
mx = nums[j]
while sum_s_w+k<mx*(j-i+1):
sum_s_w -... | frequency-of-the-most-frequent-element | [Python 3] Sliding Window Explanation with Code | vamsi81523 | 14 | 826 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,237 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1175037/Python3-binary-search | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
prefix = [0]
for x in nums: prefix.append(prefix[-1] + x)
ans = 0
for i in reversed(range(len(nums))):
lo, hi = 0, i
while lo < hi:
... | frequency-of-the-most-frequent-element | [Python3] binary search | ye15 | 6 | 543 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,238 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1175037/Python3-binary-search | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
ans = ii = sm = 0
for i in range(len(nums)):
sm += nums[i]
while k < nums[i]*(i-ii+1) - sm:
sm -= nums[ii]
ii += 1
ans = max(ans, i - ii ... | frequency-of-the-most-frequent-element | [Python3] binary search | ye15 | 6 | 543 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,239 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1833162/Python-easy-to-read-and-understand-or-sliding-window | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
sums, i, ans = 0, 0, 0
for j in range(len(nums)):
sums += nums[j]
while nums[j]*(j-i+1) > sums+k:
sums -= nums[i]
i = i+1
ans = max(ans, j-i... | frequency-of-the-most-frequent-element | Python easy to read and understand | sliding window | sanial2001 | 3 | 380 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,240 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1454840/Simple-Python-O(nlogn)-sort%2Bsliding-window-solution | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
left = right = ret = 0
window_sum = 0
while right < len(nums):
# maintain the invariant that k is enough to
# change all elements between left and right
# inclusive... | frequency-of-the-most-frequent-element | Simple Python O(nlogn) sort+sliding window solution | Charlesl0129 | 3 | 381 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,241 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2804621/Python-Prefix-Sum-with-2-Pointers%3A-O(nlogn)-time-O(n)-space | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
prefix = []
sum = 0
for i in range(len(nums)):
sum += nums[i]
prefix.append(sum)
output = 1
l = 0
for r in range(1, len(nums)):
while l < r... | frequency-of-the-most-frequent-element | Python Prefix Sum with 2 Pointers: O(nlogn) time, O(n) space | hqz3 | 0 | 8 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,242 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2699206/python-working-solution-or-90 | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
l = 0
s = 0
ans = 1
for r in range(len(nums)):
while (r-l)*nums[r] > (s+k) and l<r:
s-=nums[l]
l+=1
s+=nums[r]
ans = max(an... | frequency-of-the-most-frequent-element | python working solution | 90% | Sayyad-Abdul-Latif | 0 | 14 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,243 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2659795/python-beats-92-sort-and-sliding-window | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
l, r = 0, 0
res, total = 0, 0
for r in range(len(nums)):
total += nums[r]
while nums[r] * (r - l + 1) > total + k:
total -= nums[l]
l += 1
... | frequency-of-the-most-frequent-element | python beats 92% sort and sliding window | sahilkumar158 | 0 | 9 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,244 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2642657/Python-Easy-Solution | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
back = 0
res = 0
c = 0
for front in range(len(nums)):
# acquire---------------------------------------
c += nums[front]
# release------------------------... | frequency-of-the-most-frequent-element | Python Easy Solution | user6770yv | 0 | 15 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,245 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2628885/Sliding-window-simple-and-easy-solution | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
nums.sort()
l, r = 0, 0
total, res = 0, 0
while r < len(nums):
total += nums[r]
while (nums[r] * (r - l + 1 )) > total + k:
total -= nums[l]
... | frequency-of-the-most-frequent-element | Sliding window simple and easy solution | MaverickEyedea | 0 | 19 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,246 |
https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/2283776/Python-Short-Prefix-Sum-and-Binary-Search-with-Detailed-Explanation | class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
def freqExists(freq):
for i in range(freq, n):
if freq * nums[i-1] - (prefix_sum[i] - prefix_sum[i-freq]) <= k:
return True
return False
nums.sort()
... | frequency-of-the-most-frequent-element | [Python] Short Prefix Sum & Binary Search with Detailed Explanation | lukefall425 | 0 | 176 | frequency of the most frequent element | 1,838 | 0.386 | Medium | 26,247 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175044/Python3-greedy | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
vowels = "aeiou"
ans = 0
cnt = prev = -1
for i, x in enumerate(word):
curr = vowels.index(x)
if cnt >= 0: # in the middle of counting
if 0 <= curr - prev <= 1:
... | longest-substring-of-all-vowels-in-order | [Python3] greedy | ye15 | 9 | 703 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,248 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175044/Python3-greedy | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
ans = 0
cnt = unique = 1
for i in range(1, len(word)):
if word[i-1] <= word[i]:
cnt += 1
if word[i-1] < word[i]: unique += 1
else: cnt = unique = 1
if ... | longest-substring-of-all-vowels-in-order | [Python3] greedy | ye15 | 9 | 703 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,249 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175044/Python3-greedy | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
ans = ii = 0
unique = 1
for i in range(1, len(word)):
if word[i-1] > word[i]:
ii = i
unique = 1
elif word[i-1] < word[i]: unique += 1
if unique == 5: ... | longest-substring-of-all-vowels-in-order | [Python3] greedy | ye15 | 9 | 703 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,250 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175436/Straightforward-Python3-O(n)-stack-solution-with-explanations | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
d = {}
d['a'] = {'a', 'e'}
d['e'] = {'e', 'i'}
d['i'] = {'i', 'o'}
d['o'] = {'o', 'u'}
d['u'] = {'u'}
res, stack = 0, []
for c in word:
# If stack is empty, the firs... | longest-substring-of-all-vowels-in-order | Straightforward Python3 O(n) stack solution with explanations | tkuo-tkuo | 6 | 275 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,251 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1196458/Python3-simple-solution-using-two-pointer-approach | class Solution:
def longestBeautifulSubstring(self, s: str) -> int:
i,j,x = 0,0,0
while j < len(s):
if s[j] in ['a', 'e', 'i', 'o', 'u'] and (s[j-1] <= s[j] or j == 0):
j += 1
else:
if len(set(s[i:j])) == 5:
x = max(x,j-i)
... | longest-substring-of-all-vowels-in-order | Python3 simple solution using two pointer approach | EklavyaJoshi | 3 | 125 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,252 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1221545/Python-fast-and-simple | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
begin = None
best = 0
a_detected = False
for index, value in enumerate(word):
if not a_detected and value == 'a':
begin = index
a_detected = True
elif a_dete... | longest-substring-of-all-vowels-in-order | [Python] fast and simple | cruim | 2 | 192 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,253 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175196/Python-Bruteforce-O(n)-Simple-easy-to-follow-solution | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
s=0
prev='a'
l=[]
for char in word:
if char == 'a' and prev == 'a':
prev=char
s+=1
elif char == 'e' and (prev == 'a' or prev=='e'):
prev=char... | longest-substring-of-all-vowels-in-order | [Python] - Bruteforce O(n) - Simple easy to follow solution | ankitshah009 | 1 | 143 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,254 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2769963/JAVAPYTHON-3-EASY-INTUITIVE-CODE-WITH-EXPLANATION | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
if len(word) < 5:
return 0
ans = 0
a = e = i = o = u = False
idx = 0
while idx < len(word):
s = idx
while idx < len(word) and word[idx] == 'a':
idx += 1
... | longest-substring-of-all-vowels-in-order | [JAVA/PYTHON 3] EASY INTUITIVE CODE WITH EXPLANATION ✅ | kamdartatv1 | 0 | 7 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,255 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2686577/Python-Simple-Sliding-Window-with-HashSet-or-O(N) | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
if len(word) < 5:
return 0
l = 0
r = 0
ans = 0
seen = set()
while r < len(word):
if word[r-1] > word[r]:
l = r
seen = set(... | longest-substring-of-all-vowels-in-order | Python Simple Sliding Window with HashSet | O(N) | amany5642 | 0 | 12 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,256 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2318354/Python3-or-Sliding-Window-O(n)-with-Hashmap | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
L, R = 0, 0
output = 0
length = 0
#edge case: single letter strings1
if(len(word) < 5):
return 0
if(word[0] != 'a'):
while L < len(word) and word[L] != 'a':
... | longest-substring-of-all-vowels-in-order | Python3 | Sliding Window O(n) with Hashmap | JOON1234 | 0 | 40 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,257 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2290518/Sliding-Window-or-Python3 | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
window_size = 0
value = {'a':1,'e':2,'i':3,'o':4,'u':5}
my_set = set()
count = 1
last = word[0]
my_set.add(last)
for i in range(1,len(word)):
if value[word[i]]>... | longest-substring-of-all-vowels-in-order | Sliding Window | Python3 | user7457RV | 0 | 17 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,258 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/2102190/Python3-Solution-Noob | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
i = 0
j = 1
li = {'a':ord('a'),'e':ord('e'),'i':ord('i'),'o':ord('o'),'u':ord('u')}
l = 0
h = {}
h[word[0]] = 1
while j<len(word):
if li[word[j]]>= li[word[j-1]]:
... | longest-substring-of-all-vowels-in-order | Python3 Solution Noob | Brillianttyagi | 0 | 54 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,259 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1653693/Python-O(n)-time-O(1)-space-optimized-sliding-window-solution | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
vowels = {'a', 'e', 'i', 'o', 'u'}
next_vowel = {'a':{'a', 'e'}, 'e':{'e', 'i'}, 'i':{'i', 'o'},
'o':{'o', 'u'}, 'u':{'u'}}
res = 0
n = len(word)
if n < 5:
return 0
... | longest-substring-of-all-vowels-in-order | Python O(n) time, O(1) space optimized sliding window solution | byuns9334 | 0 | 140 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,260 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1354998/One-pass-88-speed | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
vowels = "aeiou"
idx_v = max_len = len_sub = 0
for c in word:
if not len_sub:
if c == "a":
len_sub = 1
else:
if c == vowels[idx_v]:
... | longest-substring-of-all-vowels-in-order | One pass, 88% speed | EvgenySH | 0 | 79 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,261 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1216571/Python3-Concise-Two-Pointers | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
vowels = ['a', 'e', 'i', 'o', 'u']
l, r, longest = 0, 0, 0
while (l < len(word)):
valid = True
for vowel in vowels:
valid &= (r < len(word) and word[r] == vowel)
... | longest-substring-of-all-vowels-in-order | Python3 - Concise Two Pointers | Bruception | 0 | 108 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,262 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1179961/Python-3-consider-consecutive-letters-as-one-and-find-'aeiou' | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
def convert(word):
cnt = []
s = ''
last = 'b'
cur = 0
for c in word:
if last == c:
cur += 1
else:
s += last
cnt.append(cur)
last = c
cur = 1
s += last
... | longest-substring-of-all-vowels-in-order | [Python 3] consider consecutive letters as one and find 'aeiou' | joysword | 0 | 72 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,263 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1176459/python3-Easy-to-follow-Faster-than-100 | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
# if the input string is empty
if word == '':
return 0
# we can always start from index 0
starts = [0]
# if letters are not in ascending order, the corresponding index can be a starting point
... | longest-substring-of-all-vowels-in-order | [python3] Easy to follow - Faster than 100% | zafarman | 0 | 61 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,264 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175923/Simple-Python-Solution-or-Faster-than-100 | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
next = {"": "", "a": "e", "e": "i", "i": "o", "o": "u", "u": "*"}
ans, left, past = 0, None, ""
for right in range(len(word)):
current = word[right]
if past != current:
if current =... | longest-substring-of-all-vowels-in-order | Simple Python Solution | Faster than 100% | krip_vk | 0 | 57 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,265 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175378/Python-O(n) | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
result = 0
preWord = word[0]
counter = defaultdict(int)
counter[preWord] += 1
totalVowels = 1
maxSoFar = 1
for w in word[1:]:
if preWord == w or \
... | longest-substring-of-all-vowels-in-order | Python O(n) | pochy | 0 | 43 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,266 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175166/Python-3%3A-O(n)-Simple-one-pass-solution-score-basis | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
score = { 'a': 1, 'e': 2, 'i': 3, 'o': 4, 'u': 5 }
prevScore = 0
maxBeauty = beauty = 0
seen = set()
for ch in word:
currentScore = score[ch]
if currentScore >= prevScore:
... | longest-substring-of-all-vowels-in-order | [Python 3]: O(n) Simple one pass solution - score basis | AB07 | 0 | 63 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,267 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175161/Python3%3A-Linear-scan-and-update | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
d = {"a":0, "e":1, "i":2, "o":3 , "u":4}
max_ = 0
curr_letter = 0
curr_sum = 0
sum_ = 0
for l in word:
if d[l] == curr_letter:
curr_sum += 1
... | longest-substring-of-all-vowels-in-order | Python3: Linear scan and update | zl3311 | 0 | 40 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,268 |
https://leetcode.com/problems/longest-substring-of-all-vowels-in-order/discuss/1175099/Python-simple-solution-O(n) | class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
if word == 'aeiou':
return 5
def check(s):
res = ''
ind = []
for i,c in enumerate(s):
if res =='':
res+=c
ind.append(i)
... | longest-substring-of-all-vowels-in-order | Python simple solution O(n) | deleted_user | 0 | 61 | longest substring of all vowels in order | 1,839 | 0.486 | Medium | 26,269 |
https://leetcode.com/problems/maximum-building-height/discuss/1175057/Python3-greedy | class Solution:
def maxBuilding(self, n: int, restrictions: List[List[int]]) -> int:
restrictions.extend([[1, 0], [n, n-1]])
restrictions.sort()
for i in reversed(range(len(restrictions)-1)):
restrictions[i][1] = min(restrictions[i][1], restrictions[i+1][1] + restrictio... | maximum-building-height | [Python3] greedy | ye15 | 2 | 257 | maximum building height | 1,840 | 0.353 | Hard | 26,270 |
https://leetcode.com/problems/maximum-building-height/discuss/1623986/Python-Beats-100-sort-%2B-one-pass-solution-O(nlog(n)) | class Solution:
def maxBuilding(self, n: int, restrictions: List[List[int]]) -> int:
if not restrictions:
return n - 1
restrictions.append([1, 0]) # Add the restriction for the initial position
restrictions.sort(key=lambda x: x[1] + x[0]) # Sort by increasing i + h
idx ... | maximum-building-height | [Python] Beats 100%, sort + one pass solution, O(nlog(n)) | skasch | 0 | 128 | maximum building height | 1,840 | 0.353 | Hard | 26,271 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1243646/Python3-simple-code-96-time-with-explanation | class Solution:
def replaceDigits(self, s: str) -> str:
ans = ""
def shift(char, num):
return chr(ord(char) + int(num))
for index in range(len(s)):
ans += shift(s[index-1], s[index]) if index % 2 else s[index]
return ans | replace-all-digits-with-characters | Python3 simple code 96% time, with explanation | albezx0 | 5 | 327 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,272 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1186143/Python-1-Liner-%2B-Simple-Readable | class Solution:
def replaceDigits(self, s: str) -> str:
return ''.join(chr(ord(s[i-1]) + int(s[i])) if s[i].isdigit() else s[i] for i in range(len(s))) | replace-all-digits-with-characters | Python 1 Liner + Simple Readable | leeteatsleep | 5 | 415 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,273 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1186143/Python-1-Liner-%2B-Simple-Readable | class Solution:
def replaceDigits(self, s: str) -> str:
answer = []
for i, char in enumerate(s):
if char.isdigit(): char = chr(ord(s[i-1]) + int(char))
answer.append(char)
return ''.join(answer) | replace-all-digits-with-characters | Python 1 Liner + Simple Readable | leeteatsleep | 5 | 415 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,274 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1245765/Python3-Solution | class Solution:
def replaceDigits(self, s: str) -> str:
res = ""
for index, char in enumerate(s):
# for each odd number
if index % 2 != 0 and index != 0:
# get the previous character
previous_ord = ord(s[index-1])
... | replace-all-digits-with-characters | Python3 Solution | rahulkp220 | 3 | 203 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,275 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2807747/faster-than-100-python-solution | class Solution:
def replaceDigits(self, s: str) -> str:
a = list(s)
for i in range(1, len(a), 2):
a[i] = chr(ord(a[i - 1]) + int(a[i]))
return ''.join(a) | replace-all-digits-with-characters | faster than 100% python solution | sushants007 | 2 | 57 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,276 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1276488/Python-or-Two-solutions%3A-one-liner-and-readable | class Solution:
def replaceDigits(self, s: str) -> str:
ans = list()
for i in range(len(s)):
if s[i].isdigit():
ans.append(chr(ord(s[i-1]) + int(s[i])))
else:
ans.append(s[i])
return ''.join(ans) | replace-all-digits-with-characters | Python | Two solutions: one-liner and readable | iaramer | 2 | 134 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,277 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1276488/Python-or-Two-solutions%3A-one-liner-and-readable | class Solution:
def replaceDigits(self, s: str) -> str:
return ''.join([chr(ord(s[i-1]) + int(s[i])) if s[i].isdigit() else s[i] for i in range(len(s))]) | replace-all-digits-with-characters | Python | Two solutions: one-liner and readable | iaramer | 2 | 134 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,278 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2780630/Python-simple-solution-with-the-shift-function | class Solution:
def replaceDigits(self, s: str) -> str:
s = list(s)
def shift(c, x):
alphabet = 'abcdefghijklmnopqrstuvwxyz'
index = alphabet.index(c)
return alphabet[index + x]
for i in range(1, len(s), 2):
s[i] = shift(s[i - 1], int(s[i]))
... | replace-all-digits-with-characters | Python simple solution with the shift function | Mark_computer | 1 | 6 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,279 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1185902/PythonPython3-Solution | class Solution:
def replaceDigits(self, s: str) -> str:
alpha = 'abcdefghijklmnopqrstuvwxyz'
s = list(s)
for i in range(1,len(s),2):
s[i] = alpha[(ord(s[i-1])-97)+int(s[i])]
return ''.join(s) | replace-all-digits-with-characters | Python/Python3 Solution | prasanthksp1009 | 1 | 181 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,280 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2810115/Python3-Solution-shift-with-chr-and-ord | class Solution:
def replaceDigits(self, s: str) -> str:
i = 0
ans = ""
while i < len(s):
ans = ans + s[i]
if i + 1 < len(s):
ans = ans + chr(ord(s[i]) + int(s[i+1]))
i += 2
return ans | replace-all-digits-with-characters | Python3 Solution - shift with chr and ord | sipi09 | 0 | 2 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,281 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2774280/Replace-All-Digits-with-Characters-or-PYTHON | class Solution:
def replaceDigits(self, s: str) -> str:
result=""
a="abcdefghijklmnopqrstuvwxyz"
for i in s:
if i.isdigit():
result+=a[a.index(result[-1]) + int(i)]
else:
result+=i
return result | replace-all-digits-with-characters | Replace All Digits with Characters | PYTHON | saptarishimondal | 0 | 1 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,282 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2764977/python-solution-90-faster | class Solution:
def replaceDigits(self, s: str) -> str:
alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
new_string = []
for i in range(0, len(s) - 1, 2):
new_string.append(s[i])
... | replace-all-digits-with-characters | python solution 90% faster | samanehghafouri | 0 | 2 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,283 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2743144/Python3-Utterly-unreadable-One-Liner | class Solution:
def replaceDigits(self, s: str) -> str:
return "".join([f"{char}{chr(ord(char)+int(s[idx*2+1])) if s[idx*2+1:] else ''}" for idx, char in enumerate(s[::2])]) | replace-all-digits-with-characters | [Python3] - Utterly unreadable One-Liner | Lucew | 0 | 3 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,284 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2709912/Runtime-20-ms-faster-than-91-or-Easy-to-Understand-or-Python | class Solution(object):
def replaceDigits(self, s):
alpha = {'a': 1,'b': 2,'c': 3,'d': 4,'e': 5,'f': 6,'g': 7,'h': 8,'i': 9,'j': 10,
'k': 11,'l': 12,'m': 13,'n': 14,'o': 15,'p': 16,'q': 17,'r': 18,'s': 19,'t': 20,
'u': 21,'v': 22,'w': 23,'x': 24,'y': 25,'z': 26}
num =... | replace-all-digits-with-characters | Runtime 20 ms, faster than 91% | Easy to Understand | Python | its_krish_here | 0 | 5 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,285 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2659942/Runtime%3A-30-ms-or-Memory-Usage%3A-13.8-MB | class Solution:
def replaceDigits(self, s: str) -> str:
if len(s) == 1:
return s
result = [] * len(s)
for i, j in zip(range(0, len(s), 2), range(1, len(s), 2)):
result.append(s[i])
result.append(chr(ord(s[i]) + int(s[j])))
if len(s) % 2:
... | replace-all-digits-with-characters | Runtime: 30 ms | Memory Usage: 13.8 MB | kcstar | 0 | 4 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,286 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2603903/Python-Solution | class Solution:
def replaceDigits(self, s: str) -> str:
r=''
n=len(s)
for i in range(n):
if i%2!=0:
# print(ord(s[i-1])+int(s[i]))
r+=chr(ord(s[i-1])+int(s[i]))
else:
r+=s[i]
return r | replace-all-digits-with-characters | Python Solution | Siddharth_singh | 0 | 24 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,287 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2574359/Python-easy-to-understand-solution | class Solution:
def replaceDigits(self, s: str) -> str:
r= ""
n= len(s)
for i in range(n):
if s[i].isdigit():
t= s[i-1]
k= ord(t)
m= int(s[i])
r+= chr(k+m)
else:
r+=s[i]
return r | replace-all-digits-with-characters | Python easy to understand solution | trickycat10 | 0 | 8 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,288 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2535827/Replace-All-Digits-with-Characters | class Solution:
def replaceDigits(self, s: str) -> str:
out = []
for i in range(len(s)):
if s[i].isdigit():
out.append(chr(ord(s[i-1])+int(s[i])))
else:
out.append(s[i])
return ("".join(out)) | replace-all-digits-with-characters | Replace All Digits with Characters | dhananjayaduttmishra | 0 | 8 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,289 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2324259/Short-python-solution-beats-97-(chr-and-ord) | class Solution:
def replaceDigits(self, s: str) -> str:
for i in range(len(s)):
if i%2: s = (s[:i] + (chr(ord(s[i-1]) + int(s[i]))) + s[i+1:])
return s | replace-all-digits-with-characters | Short python solution - beats 97% (chr and ord) | SabeerN | 0 | 45 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,290 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2215657/Python-simple-and-understanbale | class Solution:
def replaceDigits(self, s: str) -> str:
result = ""
for index in range(0, len(s)):
if s[index].isdigit():
result += self.shift(s[index-1], int(s[index]))
else:
result += s[index]
return result
def shift(self, c,... | replace-all-digits-with-characters | Python simple and understanbale | CleverUzbek | 0 | 35 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,291 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2038694/Faster-than-28 | class Solution:
def replaceDigits(self, s: str) -> str:
a="abcdefghijklmnopqrstuvwxyz"
b=""
for i in range(len(s)):
if(s[i].isdigit()):
b+=a[a.index(b[-1])+int(s[i])]
else:
b+=s[i]
return (b) | replace-all-digits-with-characters | Faster than 28% | Durgavamsi | 0 | 21 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,292 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/2002360/Python-O(N)-Time-Complexity | class Solution:
def replaceDigits(self, s: str) -> str:
res = []
#s = [i for i in s]
for i in range(len(s)):
if i%2 != 0:
index = ord(s[i-1]) - ord('a')
shift = chr((index + int(s[i])) % 26 + ord('a'))
res.append(shift)
... | replace-all-digits-with-characters | Python - O(N) Time Complexity | dayaniravi123 | 0 | 43 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,293 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1976505/Python-Easy-Solution-or-38ms | class Solution:
def replaceDigits(self, s: str) -> str:
final_str = ""
for i in range(len(s)):
if not s[i].isdecimal():
final_str += s[i]
else:
final_str += chr(ord(s[i-1])+int(s[i]))
return final_str | replace-all-digits-with-characters | Python Easy Solution | 38ms | pranavv14 | 0 | 76 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,294 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1927415/Python-Clean-and-Simple!-%2B-One-Liner | class Solution:
def replaceDigits(self, s):
def shift(c,x): return chr(ord(c)+int(x))
return "".join(shift(s[i-1],s[i]) if s[i].isdigit() else s[i] for i in range(len(s))) | replace-all-digits-with-characters | Python - Clean and Simple! + One-Liner | domthedeveloper | 0 | 51 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,295 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1927415/Python-Clean-and-Simple!-%2B-One-Liner | class Solution:
def replaceDigits(self, s):
return "".join(chr(ord(s[i-1])+int(s[i])) if s[i].isdigit() else s[i] for i in range(len(s))) | replace-all-digits-with-characters | Python - Clean and Simple! + One-Liner | domthedeveloper | 0 | 51 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,296 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1867277/Python-solution-with-memory-less-than-98 | class Solution:
def replaceDigits(self, s: str) -> str:
res = ""
for i in range(len(s)):
if s[i].isalpha():
res += s[i]
elif s[i].isnumeric():
res += chr(ord(s[i-1]) + int(s[i]))
return res | replace-all-digits-with-characters | Python solution with memory less than 98% | alishak1999 | 0 | 79 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,297 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1859466/Python-dollarolution | class Solution:
def replaceDigits(self, s: str) -> str:
word = ''
for i in range(len(s)):
if s[i].isalpha():
word += s[i]
continue
word += chr(ord(s[i-1])+int(s[i]))
return word | replace-all-digits-with-characters | Python $olution | AakRay | 0 | 35 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,298 |
https://leetcode.com/problems/replace-all-digits-with-characters/discuss/1764886/Replace-All-Digits-with-Characters-solution-easy | class Solution:
def replaceDigits(self, s: str) -> str:
news = ""
for i in range(1,len(s),2):
new = ord(s[i-1]) + int(s[i])
news += s[i-1] + chr(new)
if ord(s[-1]) not in range(48,58):
news += s[-1]
return news | replace-all-digits-with-characters | Replace All Digits with Characters solution easy | seabreeze | 0 | 38 | replace all digits with characters | 1,844 | 0.798 | Easy | 26,299 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.