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/construct-string-with-repeat-limit/discuss/1784750/Python-or-MaxHeap-with-comments
|
class Solution:
def repeatLimitedString(self, s: str, repeatLimit: int) -> str:
# MaxHeap method
arr = [0] * 26
result = ""
# count the num of each char
for char in s:
arr[ord(char) - ord('a')] += 1
# in the maxHeap, the largest char is on the top
maxHeap = []
for i in range(26):
if arr[i] != 0:
maxHeap.append((-i, arr[i]))
heapq.heapify(maxHeap)
while maxHeap:
# pop out the largest char
largestChar, times = heapq.heappop(maxHeap)
# to make sure we didn't add the same char in the next iteration
if result and result[-1] == chr(-largestChar + ord('a')):
return result
if times > repeatLimit:
result += repeatLimit * chr(-largestChar + ord('a'))
if maxHeap:
SecondLargestChar, times2 = heapq.heappop(maxHeap)
result += chr(-SecondLargestChar + ord('a'))
if times2 > 1:
heapq.heappush(maxHeap, (SecondLargestChar, times2-1))
heapq.heappush(maxHeap, (largestChar, times-repeatLimit))
else:
result += times * chr(-largestChar + ord('a'))
if len(result) == len(s):
return result
|
construct-string-with-repeat-limit
|
Python | MaxHeap with comments
|
Mikey98
| 0
| 48
|
construct string with repeat limit
| 2,182
| 0.519
|
Medium
| 30,300
|
https://leetcode.com/problems/count-array-pairs-divisible-by-k/discuss/1784801/Python3-factors
|
class Solution:
def coutPairs(self, nums: List[int], k: int) -> int:
factors = []
for x in range(1, int(sqrt(k))+1):
if k % x == 0: factors.append(x)
ans = 0
freq = Counter()
for x in nums:
x = gcd(x, k)
ans += freq[k//x]
for f in factors:
if x % f == 0 and f <= x//f:
freq[f] += 1
if f < x//f: freq[x//f] += 1
return ans
|
count-array-pairs-divisible-by-k
|
[Python3] factors
|
ye15
| 6
| 661
|
count array pairs divisible by k
| 2,183
| 0.287
|
Hard
| 30,301
|
https://leetcode.com/problems/count-array-pairs-divisible-by-k/discuss/1784710/Python-or-O(N-*-(k13))-or-Easy-code-with-explanation-(Get-all-factors)
|
class Solution:
def coutPairs(self, nums: List[int], k: int) -> int:
# Generate all factors of k
factors = []
for i in range(1, int(k ** 0.5) + 1):
if k % i == 0:
factors.append(i)
# To prevent us from putting the same number into it
if k // i != i:
factors.append(k // i)
res = 0
counter = collections.Counter()
for num in nums:
# `k // math.gcd(num, k)` is the smallest factor that makes `num` multiply it will be divisible by k
res += counter[k // math.gcd(num, k)]
for factor in factors:
# if num % factor == 0, means if can provide this factor for other `num` to multiply and make it divisible by k
if num % factor == 0:
counter[factor] += 1
return res
|
count-array-pairs-divisible-by-k
|
Python | O(N * (k^1/3)) | Easy code with explanation (Get all factors)
|
fishballLin
| 1
| 292
|
count array pairs divisible by k
| 2,183
| 0.287
|
Hard
| 30,302
|
https://leetcode.com/problems/count-array-pairs-divisible-by-k/discuss/1786611/Python-3-Counter-and-Prime-factorization
|
class Solution:
def countPairs(self, nums: List[int], k: int) -> int:
# prime factorization of k
def prime_factors(k):
i = 2
out = []
while pow(i, 2) <= k:
if k % i:
i += 1
else:
k //= i
out.append(i)
if k > 1:
out.append(k)
return out
pf = sorted(prime_factors(k))
# cumulative product of primes included in each num
def helper(x):
mask = 1
for f in pf:
if x < f: break
if not x % f:
mask *= f
x //= f
return mask
# num itself could be divisible by k or num containing part of primes of k
cnt = Counter(nums)
cnt_factor = defaultdict(int)
zero_factor = 0
for x in cnt:
if x % k == 0:
zero_factor += cnt[x]
else:
cnt_factor[helper(x)] += cnt[x]
# either choose two from same group or combine with the other group
ans = math.comb(zero_factor, 2) + zero_factor * (len(nums) - zero_factor)
non_zero_factor = list(cnt_factor.keys())
for i in range(len(non_zero_factor)):
a = non_zero_factor[i]
if a * a >= k and not (a * a) % k: ans += math.comb(cnt_factor[a], 2)
for j in range(i):
b = non_zero_factor[j]
if a * b >= k and not (a * b) % k:
ans += cnt_factor[a] * cnt_factor[b]
return ans
|
count-array-pairs-divisible-by-k
|
[Python 3] Counter and Prime factorization
|
chestnut890123
| 0
| 99
|
count array pairs divisible by k
| 2,183
| 0.287
|
Hard
| 30,303
|
https://leetcode.com/problems/count-array-pairs-divisible-by-k/discuss/1785034/Python-GCD-semi-brute-force-but-faster-than-100
|
class Solution:
def coutPairs(self, nums: List[int], k: int) -> int:
for i in range(len(nums)):
g = gcd(nums[i], k)
nums[i] = g if g != k else 0
c = Counter(nums)
r = 0
# 0 to other number
for i in c:
if i == 0: continue
r += c[i]
r *= c[0]
# 0 to 0
r += (c[0] * (c[0]-1)) // 2
del c[0] # 0 is dealt with
del c[1] # 1 is useless
# brute-force check for key pairs
ckeys = list(c.keys())
for a in range(len(ckeys)):
i = ckeys[a]
for b in range(a, len(ckeys)):
j = ckeys[b]
if (i * j) % k: continue
if j != i:
r += c[i] * c[j]
else:
r += (c[i] * (c[i]-1)) // 2
return r
|
count-array-pairs-divisible-by-k
|
Python GCD semi-brute-force but faster than 100%
|
antarestrue
| 0
| 72
|
count array pairs divisible by k
| 2,183
| 0.287
|
Hard
| 30,304
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1803163/Python-1-Liner-Solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum(word.find(pref) == 0 for word in words)
|
counting-words-with-a-given-prefix
|
Python 1 Liner Solution
|
anCoderr
| 3
| 210
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,305
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2221645/Python-solution-for-beginners-by-beginner.
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
ans = 0
for i in words:
if i[:len(pref)] == pref:
ans += 1
return ans
|
counting-words-with-a-given-prefix
|
Python solution for beginners by beginner.
|
EbrahimMG
| 1
| 81
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,306
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1903737/Easy-Solution-O(n)-Complexity-or-One-Liner-or-without-using-startswith-(inbuilt)-function
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum([1 for i in words if i[:len(pref)]==pref])
|
counting-words-with-a-given-prefix
|
Easy Solution O(n) Complexity | One Liner | without using startswith (inbuilt) function
|
Shewe_codes
| 1
| 82
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,307
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1842983/Python3
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
c=0
for i in range(len(words)):
if(words[i][:len(pref)])==pref:
c+=1
return c
|
counting-words-with-a-given-prefix
|
Python3
|
ExcellentProgrammer
| 1
| 32
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,308
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1841783/Simple-and-Easy-Python-Solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
pref_len=len(pref)
count=0
for i in words:
if pref==i[:pref_len]:
count+=1
return count
|
counting-words-with-a-given-prefix
|
Simple and Easy Python Solution
|
sangam92
| 1
| 39
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,309
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1803668/Python-easy-solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
# one-liner
return sum(word.find(pref) == 0 for word in words)
# detail approach
# ans = 0
# for i in range(len(words)):
# a = words[i]
# lp = len(pref)
# cnt = 0
# if len(a) < lp:
# continue
# for j in range(lp):
# if pref[j] == a[j]:
# j += 1
# cnt += 1
# if cnt == lp:
# break
# if pref[j] != a[j]:
# break
# if cnt == lp:
# ans += 1
# return ans
# take each word and check if lenght is less then continue, as its not the candidate solution
# else
# compare with pref, increment count, if count equals length of prf, its the solution, add to ans
|
counting-words-with-a-given-prefix
|
Python easy solution
|
pandeypankaj219
| 1
| 64
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,310
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1802644/Python3-1-line
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum(word.startswith(pref) for word in words)
|
counting-words-with-a-given-prefix
|
[Python3] 1-line
|
ye15
| 1
| 32
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,311
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1802552/Python-3-(70ms)-or-String-Slicing-or-Easy-to-Understand
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
k=len(pref)
c=0
for i in words:
if pref==i[:k]:
c=c+1
return c
|
counting-words-with-a-given-prefix
|
Python 3 (70ms) | String Slicing | Easy to Understand
|
MrShobhit
| 1
| 49
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,312
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2816813/Python-easy-solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
cnt = 0
for word in words:
if word.startswith(pref):
cnt += 1
return cnt
|
counting-words-with-a-given-prefix
|
Python easy solution
|
kruzhilkin
| 0
| 2
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,313
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2789041/PYTHON-ONE-LINER
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return [word[:len(pref)] for word in words].count(pref)
|
counting-words-with-a-given-prefix
|
PYTHON ONE-LINER
|
suyog_097
| 0
| 4
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,314
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2732638/Simple-solution-in-python
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
res = 0
n = len(pref)
for w in words:
if w[:n] == pref:
res += 1
return res
|
counting-words-with-a-given-prefix
|
Simple solution in python
|
ankurbhambri
| 0
| 9
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,315
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2657762/Python-oror-Easily-Understood-oror-Faster-than-96-oror-Faster
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count = 0
for i in words:
if pref == i[:len(pref)]:
count += 1
return count
|
counting-words-with-a-given-prefix
|
🔥 Python || Easily Understood ✅ || Faster than 96% || Faster
|
rajukommula
| 0
| 12
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,316
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2644698/Counting-Words-With-a-Given-Prefix-oror-Python3
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
c=0
for i in range(len(words)):
if words[i][0:len(pref)]==pref:
c+=1
return c
|
counting-words-with-a-given-prefix
|
Counting Words With a Given Prefix || Python3
|
shagun_pandey
| 0
| 2
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,317
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2616475/Python-or-Easy-or-One-Liner-Soluton
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum([pref == word[:len(pref)] for word in words])
|
counting-words-with-a-given-prefix
|
Python | Easy | One-Liner Soluton
|
anurag899
| 0
| 8
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,318
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2514692/Python3-Straightforward-approach-with-explanation
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
# Loop through each word, and compare the first len(pref) characters with
# the pref string, and add to a total if it matches
total = 0
for word in words:
if word[:len(pref)] == pref: total += 1
return total
|
counting-words-with-a-given-prefix
|
[Python3] Straightforward approach with explanation
|
connorthecrowe
| 0
| 16
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,319
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2466348/python-solutionsor-fastest-and-easy-to-understand
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
end = len(pref)
count = 0
for word in words:
if word[:end]==pref:
count += 1
return count
|
counting-words-with-a-given-prefix
|
python solutions| fastest and easy to understand
|
VikramKumarcoder
| 0
| 22
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,320
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2451434/Python-One-Liner-List-Comprehensions
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return len([i for i in words if i.startswith(pref)])
|
counting-words-with-a-given-prefix
|
Python One-Liner List Comprehensions
|
VoidCupboard
| 0
| 18
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,321
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2450560/Python-simple-solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count = 0
len_pref = len(pref)
for word in words:
if word[:len_pref] == pref:
count += 1
return count
|
counting-words-with-a-given-prefix
|
Python simple solution
|
aruj900
| 0
| 12
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,322
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2431370/2185.-Counting-Words-With-a-Given-Prefix%3A-One-Liner
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return len([ word for word in words if word[:len(pref)] == pref])
|
counting-words-with-a-given-prefix
|
2185. Counting Words With a Given Prefix: One Liner
|
rogerfvieira
| 0
| 8
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,323
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2426796/Using-bracket-operator-python-easy-faster-than-95.49
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count = 0
for word in words:
if word[:len(pref)] == pref:
count += 1
return count
|
counting-words-with-a-given-prefix
|
Using bracket operator - python easy faster than 95.49%
|
samanehghafouri
| 0
| 8
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,324
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2391443/Counting-Words-With-a-Given-Prefix
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count= 0
for i in words:
if i.startswith(pref):
count+=1
return count
|
counting-words-with-a-given-prefix
|
Counting Words With a Given Prefix
|
dhananjayaduttmishra
| 0
| 7
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,325
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2335251/Python3-Easy-One-Liner
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum([word.startswith(pref) for word in words])
|
counting-words-with-a-given-prefix
|
[Python3] Easy One-Liner
|
ivnvalex
| 0
| 30
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,326
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2285152/MYSQL-Simple-MYSQL-Solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
result = 0
prefix_length = len(pref)
for word in words:
if word[ : prefix_length] == pref:
result = result + 1
return result
|
counting-words-with-a-given-prefix
|
[ MYSQL ] ✅✅ Simple MYSQL Solution 🥳✌👍
|
ASHOK_KUMAR_MEGHVANSHI
| 0
| 9
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,327
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2161775/Simple-Python-Solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count= 0
for i in words :
if pref in i[:len(pref)]:
count +=1
return count
|
counting-words-with-a-given-prefix
|
Simple Python Solution
|
chun_chun_maru
| 0
| 19
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,328
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2152726/Time%3A-O(N)-Space%3A-O(1)
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
# iterate through each word in words
# use find to locate the prefix
# if find does not return 0 it's not a prefix
# if it does increment the result
# Time O(n) Space O(1)
res = 0
for w in words:
if w.find(pref) == 0:
res += 1
return res
|
counting-words-with-a-given-prefix
|
Time: O(N) Space: O(1)
|
andrewnerdimo
| 0
| 37
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,329
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2146191/faster-than-53.76-oror-Memory-Usage%3A-less-than-66.69-of-Python3
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count, lpref = 0, len(pref)
for i in range(len(words)):
if words[i][:lpref] == pref:
count += 1
return count
|
counting-words-with-a-given-prefix
|
faster than 53.76% || Memory Usage: less than 66.69% of Python3
|
writemeom
| 0
| 33
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,330
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2142361/Python-Solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return reduce(lambda x, y: x + (y.startswith(pref)), words, 0)
|
counting-words-with-a-given-prefix
|
Python Solution
|
hgalytoby
| 0
| 25
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,331
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2072735/Another-Python-Solution-!!
|
class Solution:
def prefixCount(self, words, pref):
count = 0
for word in words:
if word.startswith(pref):
count +=1
return count
|
counting-words-with-a-given-prefix
|
Another Python Solution !!
|
deleted_user
| 0
| 29
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,332
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2059657/Python-easy-to-read-and-understand
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
ans = 0
n = len(pref)
for word in words:
if len(word) >= n:
ans += 1 if word[:n] == pref else 0
return ans
|
counting-words-with-a-given-prefix
|
Python easy to read and understand
|
sanial2001
| 0
| 22
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,333
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/2033145/Python-oneliner
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum([x.startswith(pref) for x in words])
|
counting-words-with-a-given-prefix
|
Python oneliner
|
StikS32
| 0
| 21
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,334
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1981125/Python3-One-Line-Solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum([1 for w in words if w[:len(pref)] == pref])
|
counting-words-with-a-given-prefix
|
[Python3] One-Line Solution
|
terrencetang
| 0
| 42
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,335
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1926423/Python-Multiple-Solutions-%2B-One-Liner-%2B-Explanation-or-Clean-and-Simple!
|
class Solution:
def prefixCount(self, words, pref):
for i,p in enumerate(pref):
for j in range(len(words)-1,-1,-1):
w = words[j]
if i > len(w)-1 or w[i] != p:
del words[j]
return len(words)
|
counting-words-with-a-given-prefix
|
Python - Multiple Solutions + One Liner + Explanation | Clean and Simple!
|
domthedeveloper
| 0
| 36
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,336
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1926423/Python-Multiple-Solutions-%2B-One-Liner-%2B-Explanation-or-Clean-and-Simple!
|
class Solution:
def prefixCount(self, words, pref):
return sum(w.startswith(pref) for w in words)
|
counting-words-with-a-given-prefix
|
Python - Multiple Solutions + One Liner + Explanation | Clean and Simple!
|
domthedeveloper
| 0
| 36
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,337
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1926093/Python3-simple-solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count = 0
for i in words:
if i.startswith(pref):
count += 1
return count
|
counting-words-with-a-given-prefix
|
Python3 simple solution
|
EklavyaJoshi
| 0
| 20
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,338
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1896071/python-3-oror-easy-two-line-solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
n = len(pref)
return sum(word[:n] == pref for word in words)
|
counting-words-with-a-given-prefix
|
python 3 || easy two line solution
|
dereky4
| 0
| 29
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,339
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1878897/Python-one-liner-with-93-run-and-74-memory
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum([1 for word in words if pref == word[:len(pref)]])
|
counting-words-with-a-given-prefix
|
Python one liner with 93% run and 74% memory
|
gollapudivs
| 0
| 27
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,340
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1860141/Easy-to-understand
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
res=0
for i in words:
if i[:len(pref)]==pref:
res+=1
return res
|
counting-words-with-a-given-prefix
|
Easy to understand
|
lin11116459
| 0
| 12
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,341
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1855493/PYTHON-EASY-and-EFFICIENT-SOLUTION
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
#Base Condition
if not len(words):
return 0
'''
Check the first part of word (till the length of pref) with pref, If they are equal
increament count
'''
count = 0
prefLength = len(pref)
for word in words:
if len(word) >= prefLength and pref == word[:prefLength]:
count += 1
return count
|
counting-words-with-a-given-prefix
|
PYTHON EASY & EFFICIENT SOLUTION
|
yolo_man
| 0
| 14
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,342
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1854956/Python-quick-solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count = 0
for i in words:
if i.startswith(pref):
count += 1
return count
|
counting-words-with-a-given-prefix
|
Python quick solution
|
alishak1999
| 0
| 22
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,343
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1849997/1-Line-Python-Solution-oror-93-Faster-(40ms)-oror-Memory-less-than-40
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum([1 for word in words if word.startswith(pref)])
|
counting-words-with-a-given-prefix
|
1-Line Python Solution || 93% Faster (40ms) || Memory less than 40%
|
Taha-C
| 0
| 23
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,344
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1822880/Ez-Trie
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
trie=dict()
for w in words :
cur=trie
for ch in w :
if ch not in cur :
cur[ch]=dict()
cur=cur[ch]
cur['cnt']=1
else :
cur=cur[ch]
cur['cnt']+=1
cur=trie
for ch in pref :
if ch not in cur :
return 0
cur=cur[ch]
return cur['cnt']
|
counting-words-with-a-given-prefix
|
Ez Trie
|
P3rf3ct0
| 0
| 42
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,345
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1821053/EASY-python-solution
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
ans=0
for word in words:
if word[0:len(pref)] ==pref:
ans+=1
return ans
|
counting-words-with-a-given-prefix
|
EASY python solution
|
Buyanjargal
| 0
| 17
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,346
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1815669/2185.-Counting-Words-With-a-Given-Prefix-python-solution-simple
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
preflen = len(pref)
count = 0
for i in words:
if pref in i[:preflen]:
count += 1
return count
|
counting-words-with-a-given-prefix
|
2185. Counting Words With a Given Prefix python solution simple
|
seabreeze
| 0
| 124
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,347
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1808014/Python-One-liner-using-list-slicing
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return len([w for w in words if w[:len(pref)] == pref])
|
counting-words-with-a-given-prefix
|
[Python] One-liner using list slicing
|
casshsu
| 0
| 21
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,348
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1807573/Python-3-100-faster-then-other-solutions
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count = 0
for word in words:
if pref == word[:len(pref)]:
count+=1
return count
|
counting-words-with-a-given-prefix
|
Python 3 100% faster then other solutions
|
Sanyamx1x
| 0
| 28
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,349
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1807557/Python-Solution-oror-Slicing-Operator-Technique
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count = 0
for a in range(0, len(words)):
if words[a][:len(pref)] == pref:
count = count + 1
return count
|
counting-words-with-a-given-prefix
|
Python Solution || Slicing Operator Technique
|
UttasargaSingh
| 0
| 16
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,350
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1804701/Python-Easy-Code
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
count = 0
for i in words:
if i.startswith(pref):
count += 1
return count
|
counting-words-with-a-given-prefix
|
Python Easy Code
|
BishwashKumarSah
| 0
| 19
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,351
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1802841/Python3-1-liner-solution-using-in-built-function
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
return sum(word.startswith(pref) for word in words)
|
counting-words-with-a-given-prefix
|
[Python3] 1-liner solution using in-built function
|
__PiYush__
| 0
| 11
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,352
|
https://leetcode.com/problems/counting-words-with-a-given-prefix/discuss/1802801/python3-default-string-method-startswith
|
class Solution:
def prefixCount(self, words: List[str], pref: str) -> int:
res = 0
for i in words:
if i.startswith(pref):
res += 1
return res
|
counting-words-with-a-given-prefix
|
python3 default string method startswith
|
bianrui0315
| 0
| 16
|
counting words with a given prefix
| 2,185
| 0.771
|
Easy
| 30,353
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802652/Python3-freq-table
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
fs, ft = Counter(s), Counter(t)
return sum((fs-ft).values()) + sum((ft-fs).values())
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
[Python3] freq table
|
ye15
| 6
| 307
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,354
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802736/Self-Understandable-Python-(2-methods)-%3A
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
a=Counter(s)
b=Counter(t)
c=(a-b)+(b-a)
count=0
for i in c:
count+=c[i]
return count
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Self Understandable Python (2 methods) :
|
goxy_coder
| 3
| 164
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,355
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802736/Self-Understandable-Python-(2-methods)-%3A
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
a=Counter(s)
b=Counter(t)
count=0
for i in set(s + t):
count+=abs(a[i]-b[i])
return count
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Self Understandable Python (2 methods) :
|
goxy_coder
| 3
| 164
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,356
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1814946/Python-3-Counter-Difference-between-counts-or-Beats-91
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
count1 = [0]*26
count2 = [0]*26
for i in s:
count1[ord(i)-ord('a')] += 1
for i in t:
count2[ord(i)-ord('a')] += 1
steps = 0
for i in range(26):
steps += abs(count1[i]-count2[i])
return steps
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
[Python 3] Counter, Difference between counts | Beats 91%
|
hari19041
| 2
| 68
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,357
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802464/python3or-best-solution
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
c1,c2=Counter(s),Counter(t)
c=(c1-c2)+(c2-c1)
k=0
for i in c:
k=k+c[i]
return (k)
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
python3| best solution
|
Anilchouhan181
| 2
| 98
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,358
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1841302/Python-oror-Very-Easy-Solution-oror-using-Counter
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
common = Counter(s) & Counter(t)
count = sum(common.values())
return (len(s) - count) + (len(t) - count)
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Python || Very Easy Solution || using Counter
|
naveenrathore
| 1
| 62
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,359
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1804933/Python-Easy-Solution-or-HashMap-or-O(n)
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
hashS = {}
hashT = {}
count = 0
for i in s:
if i in hashS:
hashS[i] += 1
else:
hashS[i] = 1
for i in t:
if i in hashT:
hashT[i] += 1
else:
hashT[i] = 1
for ind, val in hashT.items():
if ind in hashS:
if val == hashS[ind]:
hashS[ind] = 0
continue
else:
count += abs(val-hashS[ind])
hashS[ind] = 0
else:
count += val
for val in hashS.values():
count += val
return count
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
[Python] Easy Solution | HashMap | O(n)
|
jamil117
| 1
| 19
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,360
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1803623/Python-easy-and-fast-solution-using-Counter
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
c1 = Counter(s)
c2 = Counter(t)
a, b = c1-c2, c2-c1
ans = 0
for i in (a+b):
ans += (a+b)[i]
return ans
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Python easy and fast solution using Counter
|
pandeypankaj219
| 1
| 36
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,361
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1803082/pythonorpython3oror-2-liner-or-easy-Fast-and-Simple!
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
s,c=Counter(s),Counter(t)
return sum(abs(s[chr(i)]-c[chr(i)]) for i in range(97,97+26))
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
python|python3|🐍| 2 liner | easy , 💨 Fast & 👌 Simple!
|
YaBhiThikHai
| 1
| 40
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,362
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802481/Python-Solution-3-lines
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
hmap_s = collections.Counter(s)
hmap_t = collections.Counter(t)
return sum((hmap_s-hmap_t).values()) + sum((hmap_t-hmap_s).values())
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Python Solution- 3 lines
|
parthberk
| 1
| 65
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,363
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/2805867/Golang-Rust-Python-using-Hashmap
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
m = {}
for i in s:
if i in m:
m[i] +=1
else:
m[i] = 1
for t1 in t:
if t1 in m:
m[t1] -= 1
else:
m[t1] = -1
res=0
for i,j in m.items():
res+=abs(j)
return res
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Golang Rust Python using Hashmap
|
anshsharma17
| 0
| 1
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,364
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/2723271/Python3-Solution-with-using-counting
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
cs = collections.Counter(s)
ct = collections.Counter(t)
res = 0
for char in cs:
if cs[char] > ct[char]:
res += cs[char] - ct[char]
for char in ct:
if ct[char] > cs[char]:
res += ct[char] - cs[char]
return res
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
[Python3] Solution with using counting
|
maosipov11
| 0
| 2
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,365
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/2723271/Python3-Solution-with-using-counting
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
c = collections.Counter(s)
for char in t:
c[char] -= 1
res = 0
for char in c:
res += abs(c[char])
return res
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
[Python3] Solution with using counting
|
maosipov11
| 0
| 2
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,366
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/2648180/Python3-Counters-or-2-Lines
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
s_ct, t_ct = Counter(s), Counter(t)
return sum(abs(s_ct[c] - t_ct[c]) for c in set(s + t))
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Python3 Counters | 2 Lines
|
ryangrayson
| 0
| 5
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,367
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/2469730/easy-python-solution
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
schar = [i for i in s]
tchar = [i for i in t]
for i in tchar :
if i not in tdict.keys() :
tdict[i] = tchar.count(i)
for i in schar :
if i not in sdict.keys() :
sdict[i] = schar.count(i)
sameCount = 0
for key in tdict.keys() :
if key in sdict.keys() :
sameCount += min(tdict[key], sdict[key])
return len(schar) - sameCount + len(tchar) - sameCount
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
easy python solution
|
sghorai
| 0
| 41
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,368
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1952619/2-Lines-Python-Solution-oror-95-Faster-oror-Memory-less-than-98
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
C1=Counter(s) ; C2= Counter(t)
return sum(((C1-C2)+(C2-C1)).values())
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
2-Lines Python Solution || 95% Faster || Memory less than 98%
|
Taha-C
| 0
| 42
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,369
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1952619/2-Lines-Python-Solution-oror-95-Faster-oror-Memory-less-than-98
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
n=len(s)+len(t)
for i in set(s): n-=min(s.count(i),t.count(i))*2
return n
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
2-Lines Python Solution || 95% Faster || Memory less than 98%
|
Taha-C
| 0
| 42
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,370
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1811084/Python3-Beats-90-Counter-and-Set
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
cs = collections.Counter(s)
ts = collections.Counter(t)
ans = 0
for c in set(cs.keys()) | set(ts.keys()):
ans += abs(cs[c]-ts[c])
return ans
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
[Python3] Beats 90% Counter and Set
|
ampl3t1m3
| 0
| 19
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,371
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1809620/Understandable-solution-using-Python-3-98.99-faster
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
d1 = Counter(s)
d2 = Counter(t)
count = 0
for item, value in d1.items(): # Iterating on d1
if item in d2: # found intersection with d2
count += abs(value - d2[item]) # substracting the intersection b/n d1 & d2
del d2[item] # deleting the intersection item in d2
else:
count += value
return count+sum(d2.values()) # count + remaining item values of d2
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Understandable solution using Python-3 98.99% faster
|
ArramBhaskar
| 0
| 37
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,372
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1804061/One-Liner-Solution-or-Python-or-Easy-Method
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
return len(list((Counter(t) - Counter(s)).elements())) + len(list((Counter(s) - Counter(t)).elements()))
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
One Liner Solution | Python | Easy Method
|
Daud-Ahmad
| 0
| 20
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,373
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802879/Easy-understand-python3-using-Hashtable
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
s_tbl = Counter(s)
t_tbl = Counter(t)
count = 0
# string s
for i in t_tbl:
if i not in s_tbl:
count += t_tbl[i]
elif t_tbl[i] > s_tbl[i]:
count += t_tbl[i]-s_tbl[i]
# string t
for i in s_tbl:
if i not in t_tbl:
count += s_tbl[i]
elif s_tbl[i] > t_tbl[i]:
count += s_tbl[i]-t_tbl[i]
return count
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Easy understand [python3] using Hashtable
|
MdKamrulShahin
| 0
| 14
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,374
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802849/Python3-Counter
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
c1, c2 = Counter(s), Counter(t)
cnt=0
for c in c1:
if c in c2:
cnt+= abs(c1[c] - c2[c])
else:
cnt+= c1[c]
for c in c2:
if c not in c1:
cnt+=c2[c]
return cnt
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
[Python3] Counter
|
__PiYush__
| 0
| 8
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,375
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802614/Python-3-(200ms)-or-Counter-HashMap-Solution-or-Easy-to-Understand
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
c1,c2=Counter(s),Counter(t)
c=(c1-c2)+(c2-c1)
k=0
for i in c:
k=k+c[i]
return (k)
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
Python 3 (200ms) | Counter HashMap Solution | Easy to Understand
|
MrShobhit
| 0
| 19
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,376
|
https://leetcode.com/problems/minimum-number-of-steps-to-make-two-strings-anagram-ii/discuss/1802474/PYTHON3-SOLUTION-USING-COUNTER
|
class Solution:
def minSteps(self, s: str, t: str) -> int:
count_s=Counter(s)
count_t=Counter(t)
count=0
for i in count_t:
if i not in count_s:
count+=count_t[i]
count_s[i]=count_t[i]
elif count_t[i]>count_s[i]:
count+=count_t[i]-count_s[i]
count_s[i]+=count_t[i]-count_s[i]
count_s[i]=count_t[i]
elif count_t[i]<count_s[i]:
count+=count_s[i]-count_t[i]
count_t[i]+=count_s[i]-count_t[i]
count_t[i]=count_s[i]
for i in count_s:
if i not in count_t:
count+=count_s[i]
count_t[i]=count_s[i]
else:
count+=abs(count_t[i]-count_s[i])
count_t[i]=count_t[i]+abs(count_t[i]-count_s[i])
return count
|
minimum-number-of-steps-to-make-two-strings-anagram-ii
|
PYTHON3 SOLUTION USING COUNTER
|
_shubham28
| 0
| 14
|
minimum number of steps to make two strings anagram ii
| 2,186
| 0.719
|
Medium
| 30,377
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1802433/Python-Solution-oror-Detailed-Article-on-Binary-Search-on-Answer
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
r = min(time) * totalTrips + 1 # This is the worst case answer possible for any case. Could use big values like 10^15 as well but they might slow the time down for smaller cases.
l = 0
ans = 0
def check_status(expected_time: int) -> int:
nonlocal ans
count = 0
for i in time:
count += expected_time // i # Total trips with time expected_time should be integer part of expected_time // i
if count < totalTrips:
return 1 # Since number of trips are less then required, left moves to mid
elif count >= totalTrips:
ans = expected_time # stores the latest result. This is guaranteed to be the minimum possible answer.
return -1 # Since number of trips are greater/equal to required, right moves to mid
while l < r-1: # Till Binary Search can continue.
mid = (l + r) // 2 # mid is the current expected time.
status = check_status(mid) # The return values 1/-1 in check_status function determines which pointer to move.
if status == 1:
l = mid
else:
r = mid
return ans
|
minimum-time-to-complete-trips
|
✅ Python Solution || Detailed Article on Binary Search on Answer
|
anCoderr
| 12
| 694
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,378
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1803087/Clearly-Explained-Python3-or-Faster-than-100-or-Numpy-%2B-Binary-Search
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
from collections import Counter
import numpy as np
dic = Counter(time)
k_arr, v_arr = np.array(list(dic.keys())), np.array(list(dic.values()))
# deal with edge cases, eg. time = [1, 1, 1, 1, 1], totalTrip = 5
if np.size(k_arr) == 1 and k_arr[0] == 1:
if totalTrips % v_arr[0] == 0: return totalTrips // v_arr[0]
else: return totalTrips // v_arr[0] + 1
# binary search
l, r = min(k_arr), min(k_arr) * totalTrips
idx = (l + r) // 2 # mid
while l + 1 < r:
temp = np.sum((idx * np.ones_like(k_arr) // k_arr) * v_arr)
if temp >= totalTrips:
r = idx
idx = (r + l) // 2
else:
l = idx
idx = (r + l) // 2
return r
|
minimum-time-to-complete-trips
|
Clearly Explained Python3 | Faster than 100% | Numpy + Binary Search
|
caffreyu
| 2
| 72
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,379
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1803087/Clearly-Explained-Python3-or-Faster-than-100-or-Numpy-%2B-Binary-Search
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
from collections import Counter
import numpy as np
dic = Counter(time)
k_arr, v_arr = np.array(list(dic.keys())), np.array(list(dic.values()))
idx, res = 1, 0
while 1:
temp = idx * np.ones_like(k_arr)
left = np.remainder(temp, k_arr)
res += sum(v_arr[left == 0])
if res >= totalTrips: return idx
idx += 1
|
minimum-time-to-complete-trips
|
Clearly Explained Python3 | Faster than 100% | Numpy + Binary Search
|
caffreyu
| 2
| 72
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,380
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1802957/Python3-oror-100-Faster-oror-Binary-Search
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
l, h = 0, min(time) * totalTrips
while l < h:
mid = (l + h) // 2
if sum([mid // i for i in time]) < totalTrips: l = mid + 1
else: h = mid
return l
|
minimum-time-to-complete-trips
|
Python3 || 100% Faster || Binary Search
|
cherrysri1997
| 2
| 59
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,381
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1802667/Python3-binary-search
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
lo, hi = 0, max(time) * totalTrips
while lo < hi:
mid = lo + hi >> 1
if sum(mid//x for x in time) < totalTrips: lo = mid + 1
else: hi = mid
return lo
|
minimum-time-to-complete-trips
|
[Python3] binary search
|
ye15
| 2
| 38
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,382
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1803324/Self-understandable-Python-%3A
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
low=0
high=max(time)*totalTrips
ans=0
while low<=high:
mid=(low+high)//2
count=0
for t in time:
count=count+(mid//t)
if count>=totalTrips:
ans=mid
high=mid-1
else:
low=mid+1
return ans
|
minimum-time-to-complete-trips
|
Self understandable Python :
|
goxy_coder
| 1
| 51
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,383
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1803094/Python3-or-Binary-Search
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
left = 0
right = max(time) * totalTrips
def can_complete_trips(mid):
trip_count = 0
for t in time:
trip_count += (mid//t)
return trip_count >= totalTrips
answer = 0
while left <= right:
mid = (left + right) >> 1
if can_complete_trips(mid):
answer = mid
right = mid-1
else:
left = mid+1
return answer
|
minimum-time-to-complete-trips
|
Python3 | Binary Search
|
suhrid
| 1
| 38
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,384
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1802800/Python3-Binary-Search-with-explanation
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
arr=[1/n for n in time]
left = math.ceil(totalTrips/sum(arr))
right = totalTrips*min(time)
while left<right:
mid=left + (right - left)//2
if sum([mid//n for n in time])>=totalTrips:
right=mid
else:
left=mid+1
return left
|
minimum-time-to-complete-trips
|
[Python3] Binary Search with explanation
|
__PiYush__
| 1
| 21
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,385
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/2826756/Python-Binary-%3A-Optimal-and-Clean-with-explanation-O(nlog(min(time)*totalTrips))-time-%3A-O(1)-space
|
class Solution:
# Hint: use binary search over the solution space. left, right = 1, min(time) * totalTrips. Given a fixed time, we can easily check to see how many trips the buses can make. If this is >= totalTrips, we set right = mid. Otherwise we set left = mid + 1.
# O(nlog(min(time)*totalTrips)) time : O(1) space
def minimumTime(self, time: List[int], totalTrips: int) -> int:
def check(mid):
return sum(mid // t for t in time) >= totalTrips
left, right = 1, min(time) * totalTrips
while left < right:
mid = left + (right-left)//2
if check(mid):
right = mid
else:
left = mid + 1
return left
|
minimum-time-to-complete-trips
|
Python Binary : Optimal and Clean with explanation - O(nlog(min(time)*totalTrips)) time : O(1) space
|
topswe
| 0
| 2
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,386
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/2766320/Python-easy-to-read-and-understand-or-binary-search
|
class Solution:
def total_trips(self, time, t, k):
trips = 0
for i in time:
trips += t//i
return trips
def minimumTime(self, time: List[int], totalTrips: int) -> int:
lo, hi = 1, 10**15
res = 0
while lo <= hi:
mid = (lo+hi) // 2
trips = self.total_trips(time, mid, totalTrips)
if trips >= totalTrips:
res = mid
hi = mid-1
else:
lo = mid+1
return res
|
minimum-time-to-complete-trips
|
Python easy to read and understand | binary-search
|
sanial2001
| 0
| 5
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,387
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/2736694/python-solution-with-comments-and-the-intuition-how-to-choose-lower-and-upper-bound
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
def isOk (curTime):
totalJourney = 0
for i in time :
totalJourney += curTime // i
return totalJourney >= totalTrips
mini = min(time)
#lets assume all the trips has been completed in 0 unit of time
l = 0
# for max bound we assume that all the trips has been performed by the bus that takes max time to complete one trip but to optimise our
#max bound we can also assume that all the trips has been performed by the bus that takes min time to complete one trip
h = mini * totalTrips
while l<=h:
mid = (l+h)//2
if( isOk( mid ) ):
h = mid-1
else:
l= mid + 1
return l
|
minimum-time-to-complete-trips
|
python solution with comments and the intuition how to choose lower and upper bound
|
mishrakripanshu303
| 0
| 6
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,388
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/2405683/Python-3-Binary-search
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
def is_possible(t):
res=0
for x in time:
res+=t//x
return res>=totalTrips
l,r=1,max(time)*totalTrips
while l<=r:
mid=l+(r-l)//2
if is_possible(mid):
res=mid
r=mid-1
else:
l=mid+1
return res
|
minimum-time-to-complete-trips
|
[Python 3] Binary search
|
gabhay
| 0
| 41
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,389
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/2384017/Fast-memory-efficient-(93)-and-simple-binary-search-algorithm-using-iteration
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
def calcTotalTrips(t:int) -> int:
tot_trips = 0
for bus_time in time:
tot_trips += (t//bus_time)
return tot_trips
start = 1
end = min(time) * totalTrips
while True:
mid = (start + end) // 2
tot_trips = calcTotalTrips(mid)
if tot_trips == totalTrips and (mid == 1 or calcTotalTrips(mid-1) < totalTrips):
return mid
if start == end - 1:
return end
if tot_trips >= totalTrips:
end = mid
else:
start = mid
|
minimum-time-to-complete-trips
|
Fast, memory efficient (93%) and simple binary search algorithm using iteration,
|
destifo
| 0
| 69
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,390
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/2383966/Simple-python3-binary-search-solution-O(nlogn)-time
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
def calcTotalTrips(t:int) -> int:
tot_trips = 0
for bus_time in time:
tot_trips += (t//bus_time)
return tot_trips
def findMinTime(start:int, end:int) -> int:
mid = (start + end) // 2
tot_trips = calcTotalTrips(mid)
if tot_trips == totalTrips and (mid == 1 or calcTotalTrips(mid-1) < totalTrips):
return mid
if start == end - 1:
return end
if tot_trips >= totalTrips:
return findMinTime(start, mid)
else:
return findMinTime(mid, end)
return findMinTime(1, min(time) * totalTrips)
|
minimum-time-to-complete-trips
|
Simple python3 binary search solution, O(nlogn) time
|
destifo
| 0
| 23
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,391
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/2047423/Very-short-and-easy-python-code
|
class Solution:
def minimumTime(self, time: List[int], total: int) -> int:
h=max(time)*total
l=1
while h>=l:
m=(h+l)//2
t=0
for i in time:
t+=(m//i)
if t>=total:
else:
l=m+1
return l
# Please upvote if you like the solution :)
|
minimum-time-to-complete-trips
|
Very short and easy python code
|
pbhuvaneshwar
| 0
| 93
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,392
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1952782/6-Lines-Python-Solution-oror-98-Faster-oror-Memory-less-than-80
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
lo=0 ; hi=totalTrips*min(time)
while lo<hi:
mid=(lo+hi)//2
if sum([mid//x for x in time])>=totalTrips: hi=mid
else: lo=mid+1
return hi
|
minimum-time-to-complete-trips
|
6-Lines Python Solution || 98% Faster || Memory less than 80%
|
Taha-C
| 0
| 77
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,393
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1815618/Python3-or-O(nlog(k))-time-or-Simple-binary-search-solution-with-explanation
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
def helper(t):
cnt = 0
for p in time:
cnt += t // p
return cnt >= totalTrips
l = 1
r = totalTrips * min(time)
while l < r:
mid = l + (r - l) // 2
if helper(mid):
r = mid
else:
l = mid + 1
return l
|
minimum-time-to-complete-trips
|
Python3 | O(nlog(k)) time | Simple binary search solution with explanation
|
Yuanbo-Peng
| 0
| 45
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,394
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1805339/Python-or-Binary-Search-or-Beat-100-in-both-time-and-space
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
# 10^5 ---> binary search
# the max time we need to complete is min(time) * totalTrips, because we want to find the minimum time
maxTime = min(time) * totalTrips
result = 0
def check(curTime):
nonlocal result
count = 0
for i in time:
count += curTime // i
# move right, need more time, i.e. left = mid
if count < totalTrips:
return True
# move left
else:
result = curTime
return False
left = 0
right = maxTime
while left <= right:
mid = left + (right - left) //2
status = check(mid)
if status:
left = mid +1
else:
right = mid-1
return result
|
minimum-time-to-complete-trips
|
Python | Binary Search | Beat 100% in both time and space
|
Mikey98
| 0
| 33
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,395
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1804234/Binary-search-80-speed
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
left, right = min(time), max(time) * totalTrips
while left < right:
middle = (left + right) // 2
if sum(middle // t for t in time) < totalTrips:
left = middle + 1
else:
right = middle
return left
|
minimum-time-to-complete-trips
|
Binary search, 80% speed
|
EvgenySH
| 0
| 15
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,396
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1803501/Python3-PriorityQueue-O(n*log(n))
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
minTime = int(totalTrips / sum(1/t for t in time))
schedule = []
trips = 0
for i, t in enumerate(time):
trips += minTime // t
schedule.append((minTime + t - (minTime % t), i)) # (nextTime, i_bus)
heapq.heapify(schedule)
while trips < totalTrips:
minTime, i = heapq.heappop(schedule)
trips += 1
heapq.heappush(schedule, (minTime + time[i], i))
return minTime
|
minimum-time-to-complete-trips
|
Python3 PriorityQueue O(n*log(n))
|
fengqifeng
| 0
| 22
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,397
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1802963/Python-Binary-Search-Approach
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
def solve(mid):
res = 0
for i in time:
res += mid//i
return res
left = 1
right = totalTrips*min(time)
while left < right:
mid = (left+right) >> 1
if solve(mid) >= totalTrips:
right = mid
else:
left = mid+1
return left
|
minimum-time-to-complete-trips
|
[Python] - Binary Search Approach ✔
|
leet_satyam
| 0
| 18
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,398
|
https://leetcode.com/problems/minimum-time-to-complete-trips/discuss/1802959/Python-3-or-Binary-Search-or-Explanation
|
class Solution:
def minimumTime(self, time: List[int], totalTrips: int) -> int:
l, r = 1, int(1e14)
def ok(mid):
nonlocal time
cnt = 0
for t in time:
cnt += mid // t
return cnt >= totalTrips
while l <= r:
mid = (l + r) // 2
if ok(mid):
r = mid - 1
else:
l = mid + 1
return l
|
minimum-time-to-complete-trips
|
Python 3 | Binary Search | Explanation
|
idontknoooo
| 0
| 52
|
minimum time to complete trips
| 2,187
| 0.32
|
Medium
| 30,399
|
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.