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/number-of-dice-rolls-with-target-sum/discuss/2650329/Python-or-DP-or-95-Space-Optimized-Solution-or
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: module = 10**9 + 7 front = [0]*(target+1) # base case.. for tar in range(target+1): if tar >= 1 and tar <= k: front[tar] = 1 else: front[tar] = 0 ...
number-of-dice-rolls-with-target-sum
Python | DP | 95% Space Optimized Solution |
quarnstric_
0
7
number of dice rolls with target sum
1,155
0.536
Medium
17,900
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2650177/Python-simple-recursion-with-memoization
class Solution: def numRollsToTarget(self, d: int, f: int, target: int) -> int: mod=(10**9)+7 memo={} def tryforother(d,f,target,memo): if target<d or target>d*f: return 0 if d==1: return 1 if target<=f else 0 if (d,f,target...
number-of-dice-rolls-with-target-sum
Python simple recursion with memoization
iliyazali
0
2
number of dice rolls with target sum
1,155
0.536
Medium
17,901
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2650115/Easy-explained-Dynamic-Programming-solution-on-Python
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: limit = 10**9 + 7 dp = [0 for x in range(target + 1)] dp[0] = 1 for roll_number in range(n): for sum_points in reversed(range(target + 1)): if sum_points < roll_number: ...
number-of-dice-rolls-with-target-sum
Easy explained Dynamic Programming solution on Python
kisel_dv
0
16
number of dice rolls with target sum
1,155
0.536
Medium
17,902
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2650053/Number-of-Dice-Rolls-With-Target-Sum-Python-or-Java
class Solution: def numRollsToTarget(self, d: int, f: int, target: int) -> int: memo = {} def dp(d, target): if d == 0: return 0 if target > 0 else 1 if (d, target) in memo: return memo[(d, target)] to_return = 0 for k i...
number-of-dice-rolls-with-target-sum
Number of Dice Rolls With Target Sum [ Python | Java ]
klu_2100031497
0
68
number of dice rolls with target sum
1,155
0.536
Medium
17,903
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2650012/Python-simple-DP-solution
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: dp = [1] + [0 for _ in range(target)] for _ in range(n): n_dp = [0 for _ in range(target+1)] for i in range(target+1): if dp[i] != 0: for j in range(1,k+1): ...
number-of-dice-rolls-with-target-sum
Python simple DP solution
AllenXia
0
3
number of dice rolls with target sum
1,155
0.536
Medium
17,904
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2649360/Python-Simple-Python-Solution
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: dp = [[-1 for j in range(target+2)] for i in range(n+1)] def cal(rem_d, tot_S): if rem_d == 0 and tot_S == 0: return 1 if tot_S < 0 or rem_d <= 0: ...
number-of-dice-rolls-with-target-sum
[ Python ] ✅ Simple Python Solution ✅✅
vaibhav0077
0
123
number of dice rolls with target sum
1,155
0.536
Medium
17,905
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2649360/Python-Simple-Python-Solution
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: dp = [[0 for j in range(target+2)] for i in range(n+1)] dp[0][0] = 1 for rem_d in range(1, n+1): for tot_S in range(0, target + 2): for a in range(1,min(k+1,tot_S + 1)): ...
number-of-dice-rolls-with-target-sum
[ Python ] ✅ Simple Python Solution ✅✅
vaibhav0077
0
123
number of dice rolls with target sum
1,155
0.536
Medium
17,906
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2649248/2D-Dynamic-programming
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: dp = [[0 for _ in range(target+1)] for _ in range(n+1)] dp[0][0] = 1 for t in range(target+1): for i in range(n): for face in range(1,k+1): if t-face >= 0: ...
number-of-dice-rolls-with-target-sum
2D Dynamic programming
chris1nexus
0
6
number of dice rolls with target sum
1,155
0.536
Medium
17,907
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2649168/Python3%3A-Faster-than-99.35
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: if target < n or target > k * n: return 0 MAX = 10 ** 9 + 7 def binom(x, y): if y > x or x < 0: return 0 if y == 0 or y == x: return 1 ...
number-of-dice-rolls-with-target-sum
Python3: Faster than 99.35%
Odinnnnnn
0
75
number of dice rolls with target sum
1,155
0.536
Medium
17,908
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2649136/Python-or-Triple-loop-DP
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: modd=10**9+7 dp=[[0 for i in range(target+1)]for j in range(n+1)] for i in range(1,min(target+1,k+1)): dp[1][i]=1 for num in range(2,n+1): for s...
number-of-dice-rolls-with-target-sum
Python | Triple loop DP
Prithiviraj1927
0
47
number of dice rolls with target sum
1,155
0.536
Medium
17,909
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2648407/Python-solution-via-backtracking-and-cache-faster-than-89
class Solution: from functools import cache @cache def numRollsToTarget(self, n: int, k: int, target: int) -> int: if target > n * k: return 0 elif target < 0 or (target == 0 and n > 0): return 0 elif target == 0 and n == 0: return 1 else...
number-of-dice-rolls-with-target-sum
Python solution via backtracking and cache faster than 89%
Terry_Lah
0
6
number of dice rolls with target sum
1,155
0.536
Medium
17,910
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2648347/Python-Bottom-up-DP
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: MOD = 10 ** 9 + 7 # DP (n, target) dp = [[0] * (target+1+k+1+1) for _ in range(n+1)] for i in range(1, k+1): dp[1][i] = 1 for i in range(1, n): for t in range(0, target+1...
number-of-dice-rolls-with-target-sum
[Python] Bottom-up DP
wtain
0
5
number of dice rolls with target sum
1,155
0.536
Medium
17,911
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2648347/Python-Bottom-up-DP
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: MOD = 10 ** 9 + 7 # DP (n, target) dp = [0] * (target+k+1) for i in range(1, k+1): dp[i] = 1 for i in range(n-1): next_dp = [0] * (target+k+1) for t in range(...
number-of-dice-rolls-with-target-sum
[Python] Bottom-up DP
wtain
0
5
number of dice rolls with target sum
1,155
0.536
Medium
17,912
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2250067/PYTHON-SOL-or-MEMO-%2B-RECURSION-or-WELL-EXPLAINED-or-EASY-or-FAST-or
class Solution: def recursion(self,n,k,target): if n == 1: # base case return 1 if 1 <= target <= k else 0 if (n,target) in self.dp: return self.dp[(n,target)] ans = 0 for i in range(1,k+1): ans += self.recursion(n-1,k,target - i) self.dp[(...
number-of-dice-rolls-with-target-sum
PYTHON SOL | MEMO + RECURSION | WELL EXPLAINED | EASY | FAST |
reaper_27
0
134
number of dice rolls with target sum
1,155
0.536
Medium
17,913
https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/discuss/2221149/Python-bottom-up-DP
class Solution: def numRollsToTarget(self, n: int, k: int, target: int) -> int: if n == 1: if target > k: return 0 else: return 1 memo = [0 for t in range(target+1)] # Note that we do not use memo[0] for i in range(1, target+1): if i <= k: ...
number-of-dice-rolls-with-target-sum
Python bottom-up DP
sticky_bits
0
113
number of dice rolls with target sum
1,155
0.536
Medium
17,914
https://leetcode.com/problems/swap-for-longest-repeated-character-substring/discuss/2255000/PYTHON-or-AS-INTERVIEWER-WANTS-or-WITHOUT-ITERTOOLS-or-WELL-EXPLAINED-or
class Solution: def maxRepOpt1(self, text: str) -> int: first_occurence,last_occurence = {},{} ans,prev,count = 1,0,0 n = len(text) for i in range(n): if text[i] not in first_occurence: first_occurence[text[i]] = i last_occurence[text[i]] = i ...
swap-for-longest-repeated-character-substring
PYTHON | AS INTERVIEWER WANTS | WITHOUT ITERTOOLS | WELL EXPLAINED |
reaper_27
1
178
swap for longest repeated character substring
1,156
0.454
Medium
17,915
https://leetcode.com/problems/swap-for-longest-repeated-character-substring/discuss/2825889/Python-Sliding-Window-O(N)
class Solution: def maxRepOpt1(self, text: str) -> int: freq = {} for ch in text: freq[ch] = freq.get(ch, 0)+1 n=len(text) i=0 distinct = 0 cur={} def include(ind): nonlocal distinct ch = text[ind] cur[c...
swap-for-longest-repeated-character-substring
Python Sliding Window O(N)
saijayavinoth
0
2
swap for longest repeated character substring
1,156
0.454
Medium
17,916
https://leetcode.com/problems/swap-for-longest-repeated-character-substring/discuss/1490882/Python-O(n)-solution
class Solution: def maxRepOpt1(self, text: str) -> int: intervals = collections.defaultdict(list) prev = '' start = 0 # collect intervals for each letter for i, ch in enumerate(text): if ch != prev and i != 0: intervals[prev].append((start, i)) ...
swap-for-longest-repeated-character-substring
Python O(n) solution
arsamigullin
0
359
swap for longest repeated character substring
1,156
0.454
Medium
17,917
https://leetcode.com/problems/swap-for-longest-repeated-character-substring/discuss/1480970/Groupby-and-Counter
class Solution: def maxRepOpt1(self, text: str) -> int: lst = [(key, len(list(seq))) for key, seq in groupby(text)] len_lst_2 = len(lst) - 2 cnt = Counter(text) max_len = 0 for i, (key, n) in enumerate(lst): max_len = max(max_len, n) if n < cnt[key]: ...
swap-for-longest-repeated-character-substring
Groupby and Counter
EvgenySH
0
99
swap for longest repeated character substring
1,156
0.454
Medium
17,918
https://leetcode.com/problems/swap-for-longest-repeated-character-substring/discuss/816103/Python-O(n)-solution-with-comments
class Solution: def maxRepOpt1(self, text: str) -> int: inuse = collections.defaultdict(int) # chars used in the repeated substring left = collections.defaultdict(int) # not used chars MOVE_TO_THE_NEXT_CHAR = 1 REPLACE = 0 LEAVE_AS_IT_IS = -1 res, i, n = 1, 0, 0 ...
swap-for-longest-repeated-character-substring
Python O(n) solution with comments
arsamigullin
0
260
swap for longest repeated character substring
1,156
0.454
Medium
17,919
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2177578/Python3-O(n2)-oror-O(1)-Runtime%3A-96ms-97.20-Memory%3A-14.5mb-84.92
class Solution: # O(n^2) || O(1) # Runtime: 96ms 97.20% Memory: 14.5mb 84.92% def countCharacters(self, words: List[str], chars: str) -> int: ans=0 for word in words: for ch in word: if word.count(ch)>chars.count(ch): break else: ...
find-words-that-can-be-formed-by-characters
Python3 O(n^2) || O(1) Runtime: 96ms 97.20% Memory: 14.5mb 84.92%
arshergon
5
350
find words that can be formed by characters
1,160
0.677
Easy
17,920
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2188726/Python-1-liner
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: return sum(len(word) if collections.Counter(word) <= collections.Counter(chars) else 0 for word in words)
find-words-that-can-be-formed-by-characters
Python 1-liner
russellizadi
2
205
find words that can be formed by characters
1,160
0.677
Easy
17,921
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2099811/Python-top-95-solution
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: ans = '' for word in words: for letter in word: if chars.count(letter) < word.count(letter): break else: ans += word return len(ans)
find-words-that-can-be-formed-by-characters
Python top 95% solution
StikS32
2
253
find words that can be formed by characters
1,160
0.677
Easy
17,922
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1875631/Python-(Simple-Approach-and-Beginner-Friendly)
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: output = 0 for i in words: count = 0 for j in i: if chars.count(j) >= i.count(j): count+=1 else: break if count ...
find-words-that-can-be-formed-by-characters
Python (Simple Approach and Beginner-Friendly)
vishvavariya
2
160
find words that can be formed by characters
1,160
0.677
Easy
17,923
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1918471/Python-One-Liner-or-Counter
class Solution: def countCharacters(self, words, chars): d, total = Counter(chars), 0 for w in words: total += self.helper(w, d.copy()) return total def helper(self, w, d): for c in w: if c not in d or d[c] == 0: return 0 else: d[c]-=1 return ...
find-words-that-can-be-formed-by-characters
Python - One-Liner | Counter
domthedeveloper
1
82
find words that can be formed by characters
1,160
0.677
Easy
17,924
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1918471/Python-One-Liner-or-Counter
class Solution: def countCharacters(self, words, chars): return (lambda c:sum(len(x) for x in words if Counter(x) < c))(Counter(chars))
find-words-that-can-be-formed-by-characters
Python - One-Liner | Counter
domthedeveloper
1
82
find words that can be formed by characters
1,160
0.677
Easy
17,925
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1806893/Python3-Solution
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: c = list(chars) l,ans = 0,0 for i in words: for j in list(i): if j in c: l += 1 c.remove(j) if l == len(i): ans += l...
find-words-that-can-be-formed-by-characters
✔Python3 Solution
Coding_Tan3
1
174
find words that can be formed by characters
1,160
0.677
Easy
17,926
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1386031/Python3-Faster-Than-99.94-Memory-Less-Than-79.96
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: from collections import Counter c = Counter(chars) cnt = 0 for word in words: good = True for letter in word: if word.count(letter) > c[letter]: ...
find-words-that-can-be-formed-by-characters
Python3 Faster Than 99.94%, Memory Less Than 79.96%
Hejita
1
109
find words that can be formed by characters
1,160
0.677
Easy
17,927
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1177497/python-sol-faster-than-96-less-mem-than-93
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: tot = 0 for i in range(len(words)): for j in range(len(words[i])): if (words[i][j] not in chars): break if (words[i].count(words[i][j]) > chars.count(words[...
find-words-that-can-be-formed-by-characters
python sol faster than 96% , less mem than 93%
elayan
1
472
find words that can be formed by characters
1,160
0.677
Easy
17,928
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2822917/Python-oror-Faster-than-99.85-and-Memory-beats-85.14
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: c = 0 for word in words: good = True for l in word: if chars.count(l) < word.count(l): good = False break if good == True: c +...
find-words-that-can-be-formed-by-characters
Python ✅✅✅|| Faster than 99.85% and Memory beats 85.14%
qiy2019
0
5
find words that can be formed by characters
1,160
0.677
Easy
17,929
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2813313/Simple-Python3-Solution-Runtime-101-ms-Beats-97.99-Memory-14.5-MB-Beats-85.3
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: result_words = [] for word in words: found = True for ch in word: if (ch not in chars) or (word.count(ch) > chars.count(ch)): found = False ...
find-words-that-can-be-formed-by-characters
Simple Python3 Solution Runtime 101 ms Beats 97.99% Memory 14.5 MB Beats 85.3%
SupriyaArali
0
4
find words that can be formed by characters
1,160
0.677
Easy
17,930
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2724771/Easy-Solution%3A-99.22-Faster
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: op=[] for i in words: na = 0 for j in i: if (j not in chars)or (i.count(j)>chars.count(j)): na = 1 break if na==0: ...
find-words-that-can-be-formed-by-characters
Easy Solution: 99.22% Faster
BAparna97
0
9
find words that can be formed by characters
1,160
0.677
Easy
17,931
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2673811/Python-solution-easy-to-understand
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: c = Counter(chars) ans = 0 for w in words: temp = Counter(w) found = True for k, v in temp.items(): if k in c: if c[k] < v: ...
find-words-that-can-be-formed-by-characters
Python solution - easy to understand
phantran197
0
11
find words that can be formed by characters
1,160
0.677
Easy
17,932
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2660999/Python%2BCounter
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: return sum([len(word) for word in words if Counter(word) <=Counter(chars) ])
find-words-that-can-be-formed-by-characters
Python+Counter
Leox2022
0
3
find words that can be formed by characters
1,160
0.677
Easy
17,933
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2602478/Python-1-liner
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: return sum(len(word) for word in words if Counter(word) <= Counter(chars))
find-words-that-can-be-formed-by-characters
Python 1-liner
Potentis
0
41
find words that can be formed by characters
1,160
0.677
Easy
17,934
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2599299/91-ms-faster-than-99.21-(with-explanation)
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: charMap, result = [0] * 26, 0 for char in chars: charMap[ord(char) - 97] += 1 for word in words: if len(word) > len(chars): continue temp = charMap[::] ...
find-words-that-can-be-formed-by-characters
91 ms, faster than 99.21% (with explanation)
kcstar
0
35
find words that can be formed by characters
1,160
0.677
Easy
17,935
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2592278/A-Double-Dictionary-Approach
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: charMap, result = {}, 0 for char in chars: charMap[char] = charMap.get(char, 0) + 1 for word in words: if len(word) > len(chars): continue temp, count = {}, 0 ...
find-words-that-can-be-formed-by-characters
A Double Dictionary Approach
kcstar
0
14
find words that can be formed by characters
1,160
0.677
Easy
17,936
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2508114/easy-Python-Solution-92-faster
class Solution(object): def countCharacters(self, words, chars): x=set(chars) ans=0 for i in words: a=set(i) if a.issubset(x): arr=[o for o in a if chars.count(o)<i.count(o)] if len(arr)==0: ans+=len(i) retur...
find-words-that-can-be-formed-by-characters
easy Python Solution 92% faster
pranjalmishra334
0
53
find words that can be formed by characters
1,160
0.677
Easy
17,937
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2395057/Easy-hashmap
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: d1={} for i in chars: if i in d1: d1[i]+=1 else: d1[i]=1 c=0 for i in words: yes=len(i) d={} for j in i: ...
find-words-that-can-be-formed-by-characters
Easy hashmap
sunakshi132
0
75
find words that can be formed by characters
1,160
0.677
Easy
17,938
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2294765/Python3-Simple-Solution
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: count = 0 d = {} dd = {} for c in chars: d[c] = d.get(c, 0) + 1 dd[c] = dd.get(c, 0) + 1 for word in words: flag = True for w in w...
find-words-that-can-be-formed-by-characters
Python3 Simple Solution
mediocre-coder
0
131
find words that can be formed by characters
1,160
0.677
Easy
17,939
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2175654/2-Easy-Methods
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: hmap1=Counter(chars) res=0 for word in words: if not (Counter(word) - hmap1): res+=len(word) return res
find-words-that-can-be-formed-by-characters
2 Easy Methods
Defence
0
42
find words that can be formed by characters
1,160
0.677
Easy
17,940
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2067337/python3-easy-solution
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: l1=[] sum=0 for i in range(len(words)): temp=list(words[i]) l1=list(chars) # print(l1) c=0 for i in range(len(temp)): if temp[i] in l1:...
find-words-that-can-be-formed-by-characters
python3 easy solution
vishwahiren16
0
90
find words that can be formed by characters
1,160
0.677
Easy
17,941
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/2043636/python-3-oror-counter-solution-oror-O(C)O(1)
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: charsCount = collections.Counter(chars) res = 0 for word in words: for c, count in collections.Counter(word).items(): if count > charsCount[c]: break el...
find-words-that-can-be-formed-by-characters
python 3 || counter solution || O(C)/O(1)
dereky4
0
176
find words that can be formed by characters
1,160
0.677
Easy
17,942
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1675247/Python3-dollarolution
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: def dicts(s): d = {} for i in s: if i not in d: d[i] = 1 else: d[i] += 1 return d s, count = 0, 0 d1 = ...
find-words-that-can-be-formed-by-characters
Python3 $olution
AakRay
0
154
find words that can be formed by characters
1,160
0.677
Easy
17,943
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1508960/Python-solution-with-explanation-(for-loop-%2B-letter-count)
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: word_char_ct = 0 for word in words: # initially assume that chars can create word boolean_ind = True # iterate thru each letter in word, if a word has MORE letters than available in ...
find-words-that-can-be-formed-by-characters
Python solution with explanation (for loop + letter count)
jjluxton
0
93
find words that can be formed by characters
1,160
0.677
Easy
17,944
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1081328/Python3-freq-table
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: fc = {} for c in chars: fc[c] = 1 + fc.get(c, 0) ans = 0 for word in words: fw = {} for c in word: fw[c] = 1 + fw.get(c, 0) if all(fw[c] <= fc.get(c, 0) for c...
find-words-that-can-be-formed-by-characters
[Python3] freq table
ye15
0
76
find words that can be formed by characters
1,160
0.677
Easy
17,945
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1081328/Python3-freq-table
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: ans = 0 fc = Counter(chars) for word in words: if not Counter(word) - fc: ans += len(word) return ans
find-words-that-can-be-formed-by-characters
[Python3] freq table
ye15
0
76
find words that can be formed by characters
1,160
0.677
Easy
17,946
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1039312/Python3-easy-solution-using-Counter
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: count = 0 for i in words: a = Counter(i) b = Counter(chars) flag = True for j in i: if not b[j] >= a[j]: flag = False if fla...
find-words-that-can-be-formed-by-characters
Python3 easy solution using Counter
EklavyaJoshi
0
130
find words that can be formed by characters
1,160
0.677
Easy
17,947
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/553095/Python-3-Solution-using-dictionary
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: good_words = [] usable_chars_dict = {} for char in chars: usable_chars_dict[char] = usable_chars_dict.get(char, 0) + 1 for word in words: available_chars = usable_chars_dict.copy() ...
find-words-that-can-be-formed-by-characters
Python 3 Solution using dictionary
duyh
0
174
find words that can be formed by characters
1,160
0.677
Easy
17,948
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1778478/6-Lines-Python-Solution-oror-Faster-than-96-oror-Memory-less-than-94
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: ans = 0 for word in words: for char in word: if word.count(char) > chars.count(char): break else: ans += len(word) return ans
find-words-that-can-be-formed-by-characters
6 Lines Python Solution || Faster than 96% || Memory less than 94%
Taha-C
-1
133
find words that can be formed by characters
1,160
0.677
Easy
17,949
https://leetcode.com/problems/find-words-that-can-be-formed-by-characters/discuss/1722868/Python-60.01-Faster-180ms
class Solution: def countCharacters(self, words: List[str], chars: str) -> int: #vars to hold values wordlen = 0 runningsum = 0 charedits = chars #evaluate words individually for word in words: wordlen = 0 charedits = chars ...
find-words-that-can-be-formed-by-characters
Python 60.01% Faster, 180ms
ovidaure
-1
172
find words that can be formed by characters
1,160
0.677
Easy
17,950
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/848959/BFS-Python-solution-with-comments!
class Solution: def maxLevelSum(self, root: TreeNode) -> int: queue = deque() #init a queue for storing nodes as we traverse the tree queue.append(root) #first node (level = 1) inserted #bfs = [] #just for understanding- this will be a bfs list to store nodes as we conduct...
maximum-level-sum-of-a-binary-tree
BFS Python solution - with comments!
tintsTy
1
58
maximum level sum of a binary tree
1,161
0.661
Medium
17,951
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/2825715/Python-DFS
class Solution: def maxLevelSum(self, root: Optional[TreeNode]) -> int: def dfs(node, level): if not node: return sums[level] += node.val dfs(node.left, level + 1) dfs(node.right, level +1 ) sums = ...
maximum-level-sum-of-a-binary-tree
Python, DFS
blue_sky5
0
2
maximum level sum of a binary tree
1,161
0.661
Medium
17,952
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/2791988/Python-solution
class Solution: def maxLevelSum(self, root: Optional[TreeNode]) -> int: levels = [] sums = float('-inf') res = 0 def levelorder(node, level): if level >= len(levels): levels.append([]) if node: levels[level...
maximum-level-sum-of-a-binary-tree
Python solution
maomao1010
0
5
maximum level sum of a binary tree
1,161
0.661
Medium
17,953
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/2431412/python-bfs-beginner-friendly
class Solution: def maxLevelSum(self, root: Optional[TreeNode]) -> int: q = collections.deque() tracker = [float('-inf'),0] q.append(root) level = 1 while q: levelSum = 0 for _ in range(len(q)): node = q.popleft() lev...
maximum-level-sum-of-a-binary-tree
python bfs beginner friendly
scr112
0
17
maximum level sum of a binary tree
1,161
0.661
Medium
17,954
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/2209920/Short-and-Very-Intuitive-with-inline-comment
class Solution: def maxLevelSum(self, root: Optional[TreeNode]) -> int: if not root: return None q=deque() max_sum=-10**18 #assume the max_level_sum to be -10**18 level=0 #this is current level level_ans=0 #this will store the level at which the max_sum ha...
maximum-level-sum-of-a-binary-tree
Short and Very Intuitive with inline-comment
Taruncode007
0
23
maximum level sum of a binary tree
1,161
0.661
Medium
17,955
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/1984012/Python-solution-100-Test-Cases
class Solution: def maxLevelSum(self, root: Optional[TreeNode]) -> int: def getHeight(root): if root.left==None and root.right==None: return 0 left=0 if root.left!=None: left = getHeight(root.left) right=0 if root.ri...
maximum-level-sum-of-a-binary-tree
Python solution 100% Test Cases
Siddharth_singh
0
25
maximum level sum of a binary tree
1,161
0.661
Medium
17,956
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/1503224/python-dfs
class Solution: def maxLevelSum(self, root: Optional[TreeNode]) -> int: stack = [(root, 1)] h = defaultdict(int) while stack: node, depth = stack.pop() if node: h[depth] += node.val stack.append((node.left, 1+depth)) ...
maximum-level-sum-of-a-binary-tree
python dfs
byuns9334
0
95
maximum level sum of a binary tree
1,161
0.661
Medium
17,957
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/1422714/PYTHON3-BFS-or-87-Fast!
class Solution: def maxLevelSum(self, root: Optional[TreeNode]) -> int: if not root : return q = deque() q.append(root) res = [] while q : sum = 0 size = len(q) for _ in range(size) : node =...
maximum-level-sum-of-a-binary-tree
PYTHON3 - BFS | 87% Fast!
athrvb
0
45
maximum level sum of a binary tree
1,161
0.661
Medium
17,958
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/1081332/Python3-bfs-by-level
class Solution: def maxLevelSum(self, root: TreeNode) -> int: ans = level = 0 val = -inf queue = [root] while queue: level += 1 newq = [] tmp = 0 for node in queue: tmp += node.val if node.left: newq.ap...
maximum-level-sum-of-a-binary-tree
[Python3] bfs by level
ye15
0
79
maximum level sum of a binary tree
1,161
0.661
Medium
17,959
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/981681/python-3-simple-solution-bfs
class Solution: def maxLevelSum(self, root: TreeNode) -> int: if not root: return None stack = [root] values = [] while stack: queue = [] for _ in range(len(stack)): node = stack.pop(0) if node.left: ...
maximum-level-sum-of-a-binary-tree
python 3 simple solution bfs
GiorgosMarga
0
45
maximum level sum of a binary tree
1,161
0.661
Medium
17,960
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/discuss/361116/Python3-level-order-BFS
class Solution: def maxLevelSum(self, root: TreeNode) -> int: from collections import deque if not root: return [] queue, res = deque([root]), [] while queue: cur_level, size = [], len(queue) for i in range(size): node = queue.popleft(...
maximum-level-sum-of-a-binary-tree
Python3 level order BFS
aj_to_rescue
0
41
maximum level sum of a binary tree
1,161
0.661
Medium
17,961
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/1158339/A-general-Explanation-w-Animation
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: # The # of rows and # of cols M, N, result = len(grid), len(grid[0]), -1 # A list of valid points valid_points = {(i, j) for i in range(M) for j in range(N)} # A double-ended queue of "land" cells ...
as-far-from-land-as-possible
A general Explanation w/ Animation
dev-josh
8
367
as far from land as possible
1,162
0.486
Medium
17,962
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/2258475/PYTHON-or-EXPLAINED-WITH-INTUTION-or-FAST-or-BFS-or-WELL-WRITTEN-or
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: n = len(grid) queue = [] vist = [[False for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): if grid[i][j] == 1: queue.append((i,j,0)) ...
as-far-from-land-as-possible
PYTHON | EXPLAINED WITH INTUTION | FAST | BFS | WELL WRITTEN |
reaper_27
4
71
as far from land as possible
1,162
0.486
Medium
17,963
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/2102946/Python-BFS.-FASTER-THAN-94.
class Solution: def maxDistance(self, grid: list[list[int]]) -> int: n = len(grid) dq = deque((i, j) for i in range(n) for j in range(n) if grid[i][j]) res = 0 while dq: r0, c0 = dq.popleft() for dr, dc in ((-1, 0), (1, 0), (0, -1), (0, 1)): r...
as-far-from-land-as-possible
Python BFS. FASTER THAN 94%.
miguel_v
4
182
as far from land as possible
1,162
0.486
Medium
17,964
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/1552850/Easy-Approach-oror-Thought-Process-oror-Well-Explained-and-Coded
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: n = len(grid) q = [] dp = [[-1 for _ in range(n)] for _ in range(n)] def isvalid(i,j): if 0<=i<n and 0<=j<n and grid[i][j]==0: return True return False for i in range(n): for j in ...
as-far-from-land-as-possible
📌📌 Easy-Approach || Thought Process || Well-Explained and Coded 🐍
abhi9Rai
4
196
as far from land as possible
1,162
0.486
Medium
17,965
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/1081337/Python3-multi-source-bfs
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: n = len(grid) # dimension ans = -1 queue = [(i, j) for i in range(n) for j in range(n) if grid[i][j]] while queue: newq = [] for i, j in queue: for ii, jj in (i-1, ...
as-far-from-land-as-possible
[Python3] multi-source bfs
ye15
2
94
as far from land as possible
1,162
0.486
Medium
17,966
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/1035927/Python-BFS-by-your-senpai
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: row, col = len(grid),len(grid[0]) queue = deque([]) water_cell = 0 for x in range(row): for y in range(col): if grid[x][y] == 1: queue.append((x,y)) else: ...
as-far-from-land-as-possible
Python BFS by your senpai
Skywalker5423
1
156
as far from land as possible
1,162
0.486
Medium
17,967
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/2846547/Python-oror-BFS-oror-easy
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: n, m = len(grid), len(grid[0]) visited = [[False for _ in range(m)] for _ in range(n)] max_distance = 0 queue = deque() for i in range(n): for j in range(m): if grid[i][j] == 1: ...
as-far-from-land-as-possible
Python || BFS || easy
dhanu084
0
1
as far from land as possible
1,162
0.486
Medium
17,968
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/2645835/Simple-Multi-Source-BFS-Solution-Python
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: n = len(grid) m = len(grid[0]) delrow = [-1,0,1,0] delcol = [0,1,0,-1] queue = [] for i in range(n): for j in range(m): if grid[i][j] == 1: ...
as-far-from-land-as-possible
Simple Multi Source BFS Solution - Python
abroln39
0
32
as far from land as possible
1,162
0.486
Medium
17,969
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/2420440/Ad-far-from-land-as-possible-oror-Python3-oror-DP
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: dist = [[math.inf] * len(grid[0]) for i in range(0, len(grid))] land = 0 for i in range(0, len(grid)): for j in range(0, len(grid[0])): if(grid[i][j] == 1): dist[i][j...
as-far-from-land-as-possible
Ad far from land as possible || Python3 || DP
vanshika_2507
0
33
as far from land as possible
1,162
0.486
Medium
17,970
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/1810940/Python-BFS
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: rows, cols = len(grid), len(grid[0]) directions = ((0, 1), (0, -1), (-1, 0), (1, 0)) # position class to represent land coordinates Position = namedtuple('Position', ['row', 'col']) ...
as-far-from-land-as-possible
Python BFS
Rush_P
0
69
as far from land as possible
1,162
0.486
Medium
17,971
https://leetcode.com/problems/as-far-from-land-as-possible/discuss/1031999/Easy-to-Read-and-Understand-Python-with-Comments!
class Solution: def maxDistance(self, grid: List[List[int]]) -> int: rows = len(grid) cols = len(grid[0]) q = collections.deque() zeros = 0 # Get our number of zeros and our 1 starting locations. for row in range(rows): for col in range(cols): ...
as-far-from-land-as-possible
Easy to Read and Understand Python with Comments!
Pythagoras_the_3rd
-1
98
as far from land as possible
1,162
0.486
Medium
17,972
https://leetcode.com/problems/last-substring-in-lexicographical-order/discuss/361321/Solution-in-Python-3-(beats-100)
class Solution: def lastSubstring(self, s: str) -> str: S, L, a = [ord(i) for i in s] + [0], len(s), 1 M = max(S) I = [i for i in range(L) if S[i] == M] if len(I) == L: return s while len(I) != 1: b = [S[i + a] for i in I] M, a = max(b), a + 1 I = [I[i] for i, j in enumera...
last-substring-in-lexicographical-order
Solution in Python 3 (beats 100%)
junaidmansuri
3
742
last substring in lexicographical order
1,163
0.35
Hard
17,973
https://leetcode.com/problems/last-substring-in-lexicographical-order/discuss/2263217/PYTHON-or-EXPLANATION-WITH-PHOTO-or-LINEAR-TIME-or-EASY-or-INTUITIVE-or
class Solution: def lastSubstring(self, s: str) -> str: n = len(s) cmax = max(s) indexes = [ i for i,c in enumerate(s) if c == cmax ] gap = 1 while len(indexes) > 1: new_indexes = [] cmax = max(s[i+gap] for i in indexes if i+gap < n) for i,...
last-substring-in-lexicographical-order
PYTHON | EXPLANATION WITH PHOTO | LINEAR TIME | EASY | INTUITIVE |
reaper_27
1
273
last substring in lexicographical order
1,163
0.35
Hard
17,974
https://leetcode.com/problems/last-substring-in-lexicographical-order/discuss/1081371/Python3-brute-force
class Solution: def lastSubstring(self, s: str) -> str: return max(s[i:] for i in range(len(s)))
last-substring-in-lexicographical-order
[Python3] brute-force
ye15
1
222
last substring in lexicographical order
1,163
0.35
Hard
17,975
https://leetcode.com/problems/last-substring-in-lexicographical-order/discuss/1081371/Python3-brute-force
class Solution: def lastSubstring(self, s: str) -> str: ii = k = 0 i = 1 while i + k < len(s): if s[ii+k] == s[i+k]: k += 1 else: if s[ii+k] > s[i+k]: i += k+1 else: ii = max(ii+k+1, i) i = ii+1...
last-substring-in-lexicographical-order
[Python3] brute-force
ye15
1
222
last substring in lexicographical order
1,163
0.35
Hard
17,976
https://leetcode.com/problems/last-substring-in-lexicographical-order/discuss/482282/Python3-two-methods
class Solution: def lastSubstring(self, s: str) -> str: i,j,k,n = 0,1,0,len(s) while j+k<n: if s[i+k]==s[j+k]: k+=1 continue elif s[i+k]>s[j+k]: j+=k+1 else: i=max(j,i+k+1) j=i+1 k=0 return s[i:] def lastSubstringBF(self, s: str) -> str: if len(s)<=1: return s i,res=1,set()...
last-substring-in-lexicographical-order
Python3 two methods
jb07
1
361
last substring in lexicographical order
1,163
0.35
Hard
17,977
https://leetcode.com/problems/last-substring-in-lexicographical-order/discuss/1073707/Python-O(n)-less-than-10-lines
class Solution: def lastSubstring(self, s: str) -> str: max_substring = "" max_char = "" for i in range(len(s)): if s[i] >= max_char: max_char = s[i] max_substring = max(max_substring, s[i : ]) return max_substring
last-substring-in-lexicographical-order
Python O(n) less than 10 lines
michaellin986
-6
630
last substring in lexicographical order
1,163
0.35
Hard
17,978
https://leetcode.com/problems/invalid-transactions/discuss/670649/Simple-clean-python-only-10-lines
class Solution: def invalidTransactions(self, transactions: List[str]) -> List[str]: invalid = [] for i, t1 in enumerate(transactions): name1, time1, amount1, city1 = t1.split(',') if int(amount1) > 1000: invalid.append(t1) continue ...
invalid-transactions
Simple clean python - only 10 lines
auwdish
7
1,600
invalid transactions
1,169
0.312
Medium
17,979
https://leetcode.com/problems/invalid-transactions/discuss/2507495/Python-Solution-or-Hashmap-or-Set
class Solution: def invalidTransactions(self, transactions: List[str]) -> List[str]: hashmap = {} #Hashset is used to skip redudant transactions being added to the result #We will only store index of the transaction because the same transaction can repeat. result = set() for i, t in enum...
invalid-transactions
Python Solution | Hashmap | Set
reeteshz
5
684
invalid transactions
1,169
0.312
Medium
17,980
https://leetcode.com/problems/invalid-transactions/discuss/2536635/Python%3A-very-simple-and-straightforward-solution
class Solution: def invalidTransactions(self, transactions: List[str]) -> List[str]: cities = defaultdict(lambda: defaultdict(list)) output = [] #build city map. for t in transactions: name, time, amount, city = t.split(',') cities[city][name].append(time)...
invalid-transactions
Python: very simple and straightforward solution
jesse14
3
1,100
invalid transactions
1,169
0.312
Medium
17,981
https://leetcode.com/problems/invalid-transactions/discuss/2827880/Python3-Linear-time-Solution-O(N)
class Solution(object): def invalidTransactions(self, transactions: List[str]) -> List[str]: # make a defaultdict to save the transactions first mapped_transactions = collections.defaultdict(lambda: collections.defaultdict(set)) # go through the transactions and save all of them in...
invalid-transactions
[Python3] - Linear time Solution O(N)
Lucew
2
45
invalid transactions
1,169
0.312
Medium
17,982
https://leetcode.com/problems/invalid-transactions/discuss/2794530/Python3-Solution-faster-than-99.90-(with-HashMap)
class Solution: def invalidTransactions(self, transactions: List[str]) -> List[str]: invalid = [] txn = collections.defaultdict(list) for trn in transactions: name, time, amount, city = trn.split(",") txn[name].append([time,amount,city]) for ...
invalid-transactions
Python3 Solution faster than 99.90% (with HashMap)
aikyab
1
36
invalid transactions
1,169
0.312
Medium
17,983
https://leetcode.com/problems/invalid-transactions/discuss/2739439/Brute-Force-Easy-To-Understand-Python-Solution
class Solution: ''' transaction: - name - time - city - amount transaction is possibly invalid if; - amount is over 1000 OR all of the following are met; - within 60 minutes of other_transaction - same name as another other_transaction ...
invalid-transactions
Brute Force Easy-To-Understand Python Solution
trietostopme
0
17
invalid transactions
1,169
0.312
Medium
17,984
https://leetcode.com/problems/invalid-transactions/discuss/2733602/Python-O(n2)-with-HashMap
class Solution: def invalidTransactions(self, transactions: List[str]) -> List[str]: nameMap = collections.defaultdict(list) ans = [] for t in transactions: splitT = t.split(",") name = splitT[0] time = splitT[1] amount = splitT[2] ...
invalid-transactions
Python O(n^2) with HashMap
chinclashs
0
40
invalid transactions
1,169
0.312
Medium
17,985
https://leetcode.com/problems/invalid-transactions/discuss/2263406/or-PYTHON-WELL-WRITTEN-or-O(N2)-or-EASY-or-FAST-or
class Solution: def invalidTransactions(self, transactions: List[str]) -> List[str]: n = len(transactions) for i in range(n): transactions[i] = transactions[i].split(',') transactions.sort(key = lambda x: int(x[1])) ans = [] failed = [False for i in range(n)] ...
invalid-transactions
| PYTHON WELL WRITTEN | O(N^2) | EASY | FAST |
reaper_27
0
204
invalid transactions
1,169
0.312
Medium
17,986
https://leetcode.com/problems/invalid-transactions/discuss/1565057/Python-O(N-%2B-KlogK)-Time-Straightforward-Python-Solutions-beats-100
class Solution: def invalidTransactions(self, transactions: List[str]) -> List[str]: N = len(transactions) # parse strings in transactions for later references data = [(n, int(t), int(m), c) for n, t, m, c in map(lambda s: s.split(','), transactions)] get_name = lambda i: data[i][...
invalid-transactions
[Python] O(N + KlogK) Time Straightforward Python Solutions, beats 100%
licpotis
0
238
invalid transactions
1,169
0.312
Medium
17,987
https://leetcode.com/problems/invalid-transactions/discuss/1084468/Python3-brute-force
class Solution: def invalidTransactions(self, transactions: List[str]) -> List[str]: flag = [False]*len(transactions) for i, transaction in enumerate(transactions): n, t, a, c = transaction.split(",") if int(a) > 1000: flag[i] = True for ii in range(i+1, len(tran...
invalid-transactions
[Python3] brute-force
ye15
0
142
invalid transactions
1,169
0.312
Medium
17,988
https://leetcode.com/problems/invalid-transactions/discuss/366512/Solution-in-Python-3
class Solution: def invalidTransactions(self, T: List[str]) -> List[str]: u, V = list(map(lambda x: [x[0],x[3],int(x[1]),int(x[2])], map(lambda x: x.split(','), T))), set() N = {i[0]:[] for i in u} for i,[n,c,t,a] in enumerate(u): N[n].append([t,c,i]) if a > 1000: V.add(T[i]) for i i...
invalid-transactions
Solution in Python 3
junaidmansuri
0
750
invalid transactions
1,169
0.312
Medium
17,989
https://leetcode.com/problems/invalid-transactions/discuss/366522/Simple-13-Line-Python-Solution
class Solution: def invalidTransactions(self, ts: List[str]) -> List[str]: nts = [t.split(',') for t in ts] nts = sorted([[a, int(b), int(c), d] for a, b, c, d in nts]) res = set() for a in nts: if a[2] > 1000: res.add(','.join(map(str,a))) for i in range(len(nts)...
invalid-transactions
Simple 13 Line Python Solution
code_report
-1
785
invalid transactions
1,169
0.312
Medium
17,990
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/401039/Python-Simple-Code-Memory-efficient
class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: def f(s): t = sorted(list(s))[0] return s.count(t) query = [f(x) for x in queries] word = [f(x) for x in words] m = [] for x in query: count = 0 for y in word: if y>x: count+=1 m.append...
compare-strings-by-frequency-of-the-smallest-character
Python Simple Code Memory efficient
saffi
14
1,700
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,991
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/1236328/Python3-Brute-Force-Solution
class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: def f(x): return x.count(min(x)) ans = [] for i in queries: count = 0 for j in words: if(f(i) < f(j)): count +...
compare-strings-by-frequency-of-the-smallest-character
[Python3] Brute Force Solution
VoidCupboard
3
77
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,992
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/1203192/Python-Easy-Solution
class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: q = [] for query in queries: query = sorted(query) temp = query.count(query[0]) q.append(temp) w = [] for word in words: word = sorted(...
compare-strings-by-frequency-of-the-smallest-character
Python Easy Solution
iamkshitij77
2
97
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,993
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/2558508/Python3-or-Solved-Using-Binary-Search-By-Translating-Each-and-Every-word-into-Function-Value
class Solution: #Let n = len(queries) and m = len(words) #Time-Complexity: O(m + mlog(m) + n*log(m)) -> O(mlog(m) + nlog(m)) #Space-Complexity: O(10*m + n*10 + m) -> O(n + m) def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: #Approach: Traverse linearly...
compare-strings-by-frequency-of-the-smallest-character
Python3 | Solved Using Binary Search By Translating Each and Every word into Function Value
JOON1234
1
20
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,994
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/416774/Python3
class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: queries_frequecy = [self.f(i) for i in queries] words_frequecy = [self.f(i) for i in words] words_frequecy.sort() res = [] length = len(words_frequecy) for i in range(l...
compare-strings-by-frequency-of-the-smallest-character
[Python3]
zhanweiting
1
235
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,995
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/2768919/python3-count-and-binary-search-sol-for-reference.
class Solution: def smallestCharFreq(self, s): sc = s[0] cnt = 1 for idx in range(1,len(s)): c = s[idx] if c < sc: cnt = 1 sc= c elif c == sc: cnt += 1 return cnt def numSmallerByFrequency(sel...
compare-strings-by-frequency-of-the-smallest-character
[python3] count and binary search sol for reference.
vadhri_venkat
0
7
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,996
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/2498001/easy-python-solution
class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: output, query_count, word_count = [], [], [] for word in queries : query_word = [ch for ch in word] query_word.sort() f_query_word = query_word.count(query_word[0]...
compare-strings-by-frequency-of-the-smallest-character
easy python solution
sghorai
0
14
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,997
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/2485310/Beginner-friendly-python-solution
class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: res = [] words = [w.count(min(w)) for w in words] queries = [q.count(min(q)) for q in queries] for q in queries: count = 0 for w in words: count...
compare-strings-by-frequency-of-the-smallest-character
Beginner friendly python solution
yhc22593
0
3
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,998
https://leetcode.com/problems/compare-strings-by-frequency-of-the-smallest-character/discuss/2263490/PYTHON-or-AS-INTERVIEWER-WANTS-or-WITHOUT-USING-PRE-DEFINED-FUNCTIONS
class Solution: def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: def freq(word): l = len(word) ans,minn = 1,word[0] for i in range(1,l): if word[i] == minn: ans += 1 elif word[i] < minn : ...
compare-strings-by-frequency-of-the-smallest-character
PYTHON | AS INTERVIEWER WANTS | WITHOUT USING PRE DEFINED FUNCTIONS
reaper_27
0
36
compare strings by frequency of the smallest character
1,170
0.614
Medium
17,999