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/concatenation-of-consecutive-binary-numbers/discuss/2613324/Python-Elegant-and-Short-or-One-line-or-Reducing
class Solution: """ Time: O(n*log(n)) Memory: O(1) """ MOD = 10 ** 9 + 7 def concatenatedBinary(self, n: int) -> int: return reduce(lambda x, y: ((x << y.bit_length()) | y) % self.MOD, range(1, n + 1))
concatenation-of-consecutive-binary-numbers
Python Elegant & Short | One line | Reducing
Kyrylo-Ktl
2
181
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,300
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/961371/Python3-1-line
class Solution: def concatenatedBinary(self, n: int) -> int: return int("".join(bin(i)[2:] for i in range(1, n+1)), 2) % 1_000_000_007
concatenation-of-consecutive-binary-numbers
[Python3] 1-line
ye15
2
134
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,301
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/961371/Python3-1-line
class Solution: def concatenatedBinary(self, n: int) -> int: ans = k = 0 for x in range(1, n+1): if not x &amp; x-1: k += 1 ans = ((ans << k) + x) % 1_000_000_007 return ans
concatenation-of-consecutive-binary-numbers
[Python3] 1-line
ye15
2
134
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,302
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613430/python-one-line-solution
class Solution: def concatenatedBinary(self, n: int) -> int: return int("".join([bin(i)[2:] for i in range(1, n+1)]), base=2) % 1000000007
concatenation-of-consecutive-binary-numbers
python one-line solution
secretbit
1
42
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,303
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612449/python3-or-3-lines-of-code-or-easy
class Solution: def concatenatedBinary(self, n: int) -> int: bin_str = '' for i in range(1, n+1): bin_str += bin(i)[2:] return int(bin_str, 2)%(10**9 + 7)
concatenation-of-consecutive-binary-numbers
python3 | 3 lines of code | easy
H-R-S
1
154
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,304
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612294/Easy-Python-Solution-(4-lines)
class Solution: def concatenatedBinary(self, n: int) -> int: ans = "" for i in range(1, n + 1): ans += bin(i)[2:] return int(ans, 2) % (1000000000 + 7)
concatenation-of-consecutive-binary-numbers
Easy Python Solution (4 lines)
Dayeon
1
12
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,305
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612164/One-Liner-explanation-and-code-oror-Python3-Solution-and-Explanation
class Solution: def concatenatedBinary(self, n: int) -> int: return int("".join([bin(i)[2:] for i in range(1,n+1)]),2)%1000000007
concatenation-of-consecutive-binary-numbers
💚One-Liner explanation and code || Python3 Solution and Explanation
Rage-Fox
1
70
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,306
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2473571/python-solution-oror-easy-to-understand-oror-beginner-friendly
class Solution: def concatenatedBinary(self, n: int) -> int: x="" for i in range(1,n+1): x+=bin(i).replace("0b","") return int(x,2)%(10**9+7)
concatenation-of-consecutive-binary-numbers
python solution || easy to understand || beginner friendly🤞✔
shane6123
1
54
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,307
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/1037832/PythonPython3-Concatenation-of-Consecutive-Binary-Numbers
class Solution: def concatenatedBinary(self, n: int) -> int: final_number = '' for x in range(1, n+1): final_number += bin(x)[2:] return int(final_number, 2) % (10**9 + 7)
concatenation-of-consecutive-binary-numbers
[Python/Python3] Concatenation of Consecutive Binary Numbers
newborncoder
1
123
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,308
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/961626/Python-1-liner
class Solution: def concatenatedBinary(self, n: int) -> int: ans='' for i in range(n+1): ans+=bin(i)[2:] return int(ans,2)%(10**9+7)
concatenation-of-consecutive-binary-numbers
Python 1-liner
lokeshsenthilkumar
1
106
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,309
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/961626/Python-1-liner
class Solution: def concatenatedBinary(self, n: int) -> int: return int(''.join([bin(i)[2:] for i in range(n+1)]),2)%(10**9+7)
concatenation-of-consecutive-binary-numbers
Python 1-liner
lokeshsenthilkumar
1
106
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,310
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2837562/Brutish-Python-Answer-that-fits-in-time-limits
class Solution: def concatenatedBinary(self, n: int) -> int: # get modulo of the current value modulo_value = 10**9 + 7 # build a string array binary_array = [] # for value in range 1 to n+1 so you include n for value in range(1, n+1) : # take the string binary version of it from 2nd indice forward binary_array.append(str(bin(value))[2:]) # concat them all binary_string_of_val = ''.join(binary_array) # return int cast of the string from binary modulo the modulo value return int(binary_string_of_val, 2) % modulo_value
concatenation-of-consecutive-binary-numbers
Brutish Python Answer that fits in time limits
laichbr
0
1
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,311
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2829182/Runtime-Beats-79.44-Memory-Beats-81.27
class Solution: def concatenatedBinary(self, n: int) -> int: t = 1 c = 0 for i in range(2,n+1): c = len(str(bin(i)))-2 tshift = t<<c t = (tshift+i)%(10**9+7) return t
concatenation-of-consecutive-binary-numbers
Runtime Beats 79.44% Memory Beats 81.27%
Sashaghoori
0
1
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,312
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2822258/Easy-single-line-Python
class Solution: def concatenatedBinary(self, n: int) -> int: return int("".join([bin(x)[2:] for x in range(1, n+1)]), 2) % (10**9 + 7)
concatenation-of-consecutive-binary-numbers
Easy single-line Python
nickheyer
0
1
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,313
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2724162/Simple-python-code-with-explanation
class Solution: def concatenatedBinary(self, n): #create a empty sting s to store the contatination of binary values from 1 to n s = '' #iterate forloop from 1 to n for i in range(1,n+1): #convert each number to binary k= bin(i) #for i == 2: #k will be 0b10 #binary number starts from second index #so add the binary number k[2:] to string (s) s = s+ k[2:] #after ending forloop #s contains all the binary numbers from 1 to n #change that string s to integer res = int(s,2) #value of ans will be more so do modulo (10^9 + 7) return res%(1000000007)
concatenation-of-consecutive-binary-numbers
Simple python code with explanation
thomanani
0
5
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,314
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2623313/python3-Bit-shifting-solution-for-reference.
class Solution: def concatenatedBinary(self, n: int) -> int: MOD = (10**9+7) ans = 0 powerOf2 = 0 p2v = 2**powerOf2 for i in range(1, n+1): if i >= p2v: powerOf2 += 1 p2v = 2**powerOf2 ans = ans << powerOf2 ans += i ans = ans % MOD return ans % MOD
concatenation-of-consecutive-binary-numbers
[python3] Bit shifting solution for reference.
vadhri_venkat
0
16
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,315
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2622373/Python-Solution
class Solution: def concatenatedBinary(self, n: int) -> int: l=[] for i in range(1,n+1): a=bin(i)[2:] #print(a) l.append(a) #print(l) n=''.join(l) n=int(n,2) return int(n)%((10**9)+7)
concatenation-of-consecutive-binary-numbers
Python Solution
shagun_pandey
0
12
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,316
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2614561/Easy-Python3-Solution
class Solution: def concatenatedBinary(self, n: int) -> int: result = "" for i in range(1,n+1): result += bin(i)[2:] return int(result, 2) % ((10**9)+7)
concatenation-of-consecutive-binary-numbers
Easy Python3 Solution
leetcodeninja
0
25
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,317
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2614556/Python3-oror-Illegal-Solution-oror-But-Fast
class Solution: def concatenatedBinary(self, n: int) -> int: s = '' for i in range(1, n+1): s += bin(i).replace('0b', '') return int(s, 2) % (10**9 + 7)
concatenation-of-consecutive-binary-numbers
Python3 || Illegal Solution || But Fast
Dewang_Patil
0
14
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,318
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2614374/O(n)-using-bit-counting-and-simulation
class Solution: def concatenatedBinary(self, n: int) -> int: mod = 10**9 + 7 ans = 0 k, bits = 31, 1 for ni in range(n, 0, -1): ans = ans + (ni * bits) % mod while ni>>k==0: k -= 1 bits = (bits * (1 << (k + 1))) % mod ans = ans % mod return ans
concatenation-of-consecutive-binary-numbers
O(n) using bit counting and simulation
dntai
0
20
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,319
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2614162/Python-Solution
class Solution: def concatenatedBinary(self, n: int) -> int: M = 10 ** 9 + 7 s = "" for i in range(1, n + 1): s += "{0:b}".format(i) return int(s, 2) % M
concatenation-of-consecutive-binary-numbers
Python Solution
Harshi0109
0
16
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,320
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2614088/Python-one-liner-easy-and-simple-solution
class Solution: def concatenatedBinary(self, n: int) -> int: return int("".join([format(i,"b") for i in range(1,n+1)]),2)%(10**9+7)
concatenation-of-consecutive-binary-numbers
Python one liner easy and simple solution
SouravSingh49
0
9
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,321
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613815/Python-Readable-bit-Manipulation
class Solution: def concatenatedBinary(self, n: int) -> int: MOD = 10**9+7 # this is a shifted binary problem # we need to figure out, how many binary # digits the number has # we can keep track of the bit length of # the number as it will be increasing # # every time we encounter a number that # is a power of two, we increase the bit # length result = 0 curr_but_length = 0 for num in range(1, n+1): # check whether our bit length increased # num &amp; (num-1) deletes the lowest set bit # if there is no bit left after deletion # we have a power of two and need to increase # our bit length if num &amp; (num-1) == 0: curr_but_length += 1 # shift the result upwards for the number of binary digits # add the current number (by just or, as there will be only # zeros in the lower bits after shifting) result = ((result << (curr_but_length)) | num) % MOD return result
concatenation-of-consecutive-binary-numbers
[Python] - Readable bit Manipulation
Lucew
0
6
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,322
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613594/Python-simple-solution-2657-ms-15.8-MB
class Solution: def concatenatedBinary(self, n: int) -> int: binary_string = "" for i in range(1, n+1): binary_string += bin(i)[2:] return int(binary_string, 2) % (10**9 + 7)
concatenation-of-consecutive-binary-numbers
Python simple solution # 2657 ms 15.8 MB
remenraj
0
8
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,323
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613566/Python-or-O(n)-97.64-or-simulation-or-bit-manipultion
class Solution: def concatenatedBinary(self, n: int) -> int: modulo = 10 ** 9 + 7 shift = 0 # tracking power of 2 res = 0 for i in range(1, n+1): if i &amp; (i - 1) == 0: # see if num reaches a greater power of 2 shift += 1 res = ((res << shift) + i) % modulo # do the simulation return res
concatenation-of-consecutive-binary-numbers
Python | O(n), 97.64% | simulation | bit manipultion
MajimaAyano
0
29
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,324
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613468/concatenation-of-consecutive-binary-numbers
class Solution: def concatenatedBinary(self, n: int) -> int: mod=10**9+7 l=[bin(i)[2:] for i in range(1,n+1)] return int(''.join(l),2)%mod
concatenation-of-consecutive-binary-numbers
concatenation-of-consecutive-binary-numbers
shivansh2001sri
0
7
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,325
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613359/Python-solution-oror-Short-and-Easy-oror-Using-Bit-manipulation
class Solution: def concatenatedBinary(self, n: int) -> int: places_shifted = 0 ans = 0 for num in range(1, n+1): # places_shifted is the number of bits if num &amp; (num-1) == 0: places_shifted += 1 # ans will be shifted by places_shifted and num will be added ans = ((ans << places_shifted) + num) % (10**9 + 7) return ans
concatenation-of-consecutive-binary-numbers
Python solution || Short and Easy || Using Bit manipulation
vanshika_2507
0
12
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,326
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613202/Python3-Solution-or-O(n)-or-Bit-Manipulation
class Solution: def concatenatedBinary(self, n): ans, l, mod = 0, 0, 1000_000_007 for i in range(1, n + 1): if i &amp; (i-1) == 0: l += 1 ans = ((ans << l) + i) % mod return ans
concatenation-of-consecutive-binary-numbers
✔ Python3 Solution | O(n) | Bit Manipulation
satyam2001
0
18
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,327
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2613031/Python-or-Brute-Force-One-liner-or-Do-not-do-this-at-home
class Solution: def concatenatedBinary(self, n: int) -> int: return int("".join(bin(i)[2:] for i in range(1, n + 1)), 2) % (10 ** 9 + 7)
concatenation-of-consecutive-binary-numbers
Python | Brute Force One-liner | Do not do this at home
sr_vrd
0
3
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,328
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612989/Python-or-Simplest-Soln-or-Easy-Approch
class Solution: def concatenatedBinary(self, n: int) -> int: s="" for i in range(1,n+1): s+= str(bin(i)[2:]) return int(s,2)%(10**9+7)
concatenation-of-consecutive-binary-numbers
Python | Simplest Soln | Easy Approch 💯✅
adarshg04
0
15
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,329
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612928/Python-Solution-Easy-to-understand-using-bin-function
class Solution: def concatenatedBinary(self, n: int) -> int: res = "" # iterateing till the limit n for i in range(n+1): # getting the binary value for each one using bin # storing it in the variable # remove the 0b in front of the binary value which is used to # contrast binary from decimal res += bin(i).replace('0b', '') # at last converting it into decimal using int() and 2 represents the base value # return int(res, 2)%(10**9+7)**
concatenation-of-consecutive-binary-numbers
Python Solution Easy to understand using bin function
Sleur
0
4
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,330
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612877/GolangPython-O(N)-time-or-O(1)-space
class Solution: def concatenatedBinary(self, n: int) -> int: s = 0 for i in range(1, n+1): s = (s << i.bit_length() | i) % 1000000007 return s
concatenation-of-consecutive-binary-numbers
Golang/Python O(N) time | O(1) space
vtalantsev
0
21
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,331
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612791/Python-3-Line-Self-Explanatory
class Solution: def concatenatedBinary(self, n: int) -> int: ans='' for i in range(1,n+1): ans+=bin(i)[2:] return int(ans,2)%(10**9+7)
concatenation-of-consecutive-binary-numbers
🔥🔥🔥Python ,3-Line Self Explanatory🔥🔥🔥
lakshayne
0
13
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,332
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612783/Python-or-Self-explanatory-or-easy-to-understand
class Solution: def concatenatedBinary(self, n: int) -> int: temp="" for i in range(1,n+1): temp_bin=bin(i)[2:] temp+=temp_bin temp=int(temp,2) return temp % (10**9+7)
concatenation-of-consecutive-binary-numbers
Python | Self-explanatory | easy to understand
Mom94
0
11
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,333
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612726/Python-Solution-or-Simple-or-Brute-Force%3A-Accepted
class Solution: def concatenatedBinary(self, n: int) -> int: s='' for i in range(1, n+1): s+=bin(i).replace("0b", "") # print(s) return int(s,2)%1000000007
concatenation-of-consecutive-binary-numbers
Python Solution | Simple | Brute Force: Accepted
Siddharth_singh
0
10
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,334
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612563/python3-oror-simple-and-easy-oror-2-line
class Solution: def concatenatedBinary(self, n: int) -> int: x = "".join(bin(i)[2:] for i in range(1, n+1)) return (int(x, 2) % (10**9+7))
concatenation-of-consecutive-binary-numbers
python3 || simple and easy || 2 line
Exoutia
0
21
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,335
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612521/Easy-to-understand-One-line-solution
class Solution: def concatenatedBinary(self, n: int) -> int: return int(''.join([format(i,'b') for i in range(n+1)]),2) %(10**9+7)
concatenation-of-consecutive-binary-numbers
Easy to understand One line solution
Endale
0
6
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,336
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612513/Python-Solution
class Solution: def concatenatedBinary(self, n: int) -> int: sr = "" for i in range(1,n+1): sr += bin(i)[2:] return int(sr,2)%(10**9+7)
concatenation-of-consecutive-binary-numbers
Python Solution
a_dityamishra
0
13
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,337
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612423/Python-One-Line-Solution
class Solution: def concatenatedBinary(self, n: int) -> int: return int("".join(bin(i+1)[2:] for i in range(n)), 2) % (10**9+7)
concatenation-of-consecutive-binary-numbers
Python One Line Solution
cheetah5
0
20
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,338
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612416/python3-easy-solution(4-line)
class Solution: def concatenatedBinary(self, n: int) -> int: # first get the binary value of each number and concatinate s="" for x in range(1,n+1): s+=bin(x)[2:] # now change the concatinated binary to decimal and do the moudulo operation return(int(s,2)%(10**9+7))
concatenation-of-consecutive-binary-numbers
python3 easy solution(4 line)
priyam_jsx
0
15
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,339
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612372/python-easy-approach
class Solution: def concatenatedBinary(self, n: int) -> int: binary = "" for i in range(1,n+1): #convert the decimal to binary bindata = format(i,"b") #add them together binary += bindata #convert binary to decimal ans = int(binary,2) return ans % (10**9 + 7)
concatenation-of-consecutive-binary-numbers
python easy approach
Zao_Needs_Buff
0
9
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,340
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/2612148/Python-Solution-or-one-liner
class Solution: def concatenatedBinary(self, n: int) -> int: return int(''.join('{0:b}'.format(i) for i in range(1, n+1)), 2) % (10 ** 9 + 7)
concatenation-of-consecutive-binary-numbers
Python Solution | one-liner
everydayspecial
0
29
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,341
https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/discuss/1831270/Python-Solution
class Solution: def concatenatedBinary(self, n: int) -> int: s='' for i in range(1, n+1): s+=bin(i).replace("0b", "") # print(s) return int(s,2)%1000000007
concatenation-of-consecutive-binary-numbers
Python Solution
Siddharth_singh
0
38
concatenation of consecutive binary numbers
1,680
0.57
Medium
24,342
https://leetcode.com/problems/minimum-incompatibility/discuss/965262/Python3-backtracking
class Solution: def minimumIncompatibility(self, nums: List[int], k: int) -> int: nums.sort() def fn(i, cand): """Populate stack and compute minimum incompatibility.""" nonlocal ans if cand + len(nums) - i - sum(not x for x in stack) > ans: return if i == len(nums): ans = cand else: for ii in range(k): if len(stack[ii]) < len(nums)//k and (not stack[ii] or stack[ii][-1] != nums[i]) and (not ii or stack[ii-1] != stack[ii]): stack[ii].append(nums[i]) if len(stack[ii]) == 1: fn(i+1, cand) else: fn(i+1, cand + stack[ii][-1] - stack[ii][-2]) stack[ii].pop() ans = inf stack = [[] for _ in range(k)] fn(0, 0) return ans if ans < inf else -1
minimum-incompatibility
[Python3] backtracking
ye15
3
156
minimum incompatibility
1,681
0.374
Hard
24,343
https://leetcode.com/problems/minimum-incompatibility/discuss/965262/Python3-backtracking
class Solution: def minimumIncompatibility(self, nums: List[int], k: int) -> int: nums.sort() def fn(i, cand=0, ans=inf): """Return minimum incompatibility via backtracking.""" if cand + len(nums) - i - sum(not x for x in stack) >= ans: return ans # early return if i == len(nums): return cand for ii in range(k): if len(stack[ii]) < len(nums)//k and (not stack[ii] or stack[ii][-1] != nums[i]) and (not ii or stack[ii-1] != stack[ii]): stack[ii].append(nums[i]) if len(stack[ii]) == 1: ans = fn(i+1, cand, ans) else: ans = fn(i+1, cand + stack[ii][-1] - stack[ii][-2], ans) stack[ii].pop() return ans stack = [[] for _ in range(k)] ans = fn(0) return ans if ans < inf else -1
minimum-incompatibility
[Python3] backtracking
ye15
3
156
minimum incompatibility
1,681
0.374
Hard
24,344
https://leetcode.com/problems/minimum-incompatibility/discuss/962339/Python-or-2-solutions-or-1-Brute-force-or-1-Memoization-(AC)
class Solution: def minimumIncompatibility(self, nums: List[int], k: int) -> int: nums.sort() x = len(nums)//k combos = [] for i in itertools.combinations(nums, x): if len(set(i)) == x: combos.append(i) result = float('inf') for i in itertools.combinations(combos, k): if sorted([y for x in i for y in x]) == nums: score = sum([max(x) - min(x) for x in i]) result = min(score, result) return result if result != float("inf") else -1
minimum-incompatibility
Python | 2 solutions | 1 Brute force | 1 Memoization (AC)
dev-josh
0
69
minimum incompatibility
1,681
0.374
Hard
24,345
https://leetcode.com/problems/minimum-incompatibility/discuss/962339/Python-or-2-solutions-or-1-Brute-force-or-1-Memoization-(AC)
class Solution: def minimumIncompatibility(self, nums: List[int], k: int) -> int: partition_len = len(nums) // k @functools.lru_cache(maxsize=None) def recurse(nums): if not nums: return 0 result = float('inf') for combo in itertools.combinations(nums, partition_len): if len(set(combo)) < partition_len: continue updated_nums = list(nums) for i in combo: updated_nums.remove(i) result = min( result, max(combo) - min(combo) + recurse(tuple(updated_nums)) ) return result result = recurse(tuple(nums)) return result if result != float('inf') else -1
minimum-incompatibility
Python | 2 solutions | 1 Brute force | 1 Memoization (AC)
dev-josh
0
69
minimum incompatibility
1,681
0.374
Hard
24,346
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1054303/Python-simple-one-liner
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: return sum(set(allowed) >= set(i) for i in words)
count-the-number-of-consistent-strings
Python - simple one liner
angelique_
12
828
count the number of consistent strings
1,684
0.819
Easy
24,347
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1577520/python-sol-using-sorted()-and-set()
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = 0 for i in range(len(words)): if sorted(set(list(words[i] + allowed))) == list(sorted(allowed)): count += 1 return count
count-the-number-of-consistent-strings
python sol using sorted() and set()
anandanshul001
6
567
count the number of consistent strings
1,684
0.819
Easy
24,348
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1347474/Python-solution-%3A-EXPLAINED-%3A-with-fastest-in-time(90)-and-lowest(99.5)-in-space.
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: """Brute Force Time complexity : O(n*m) where m = max(len(word) in words) Space complexity: O(1) """ count = 0 for word in words: for ch in word: if ch not in allowed: count += 1 break return len(words)-count """One liner Set operation """ return sum(not set(word)-set(allowed) for word in words) """Set operation by subset operation NOTE : SPACE COMPLEXITY INCREASES IN THIS APPROACH. """ allowed = set(allowed) n = 0 for w in words: if set(w).issubset(allowed): n += 1 return n
count-the-number-of-consistent-strings
Python solution : EXPLAINED : with fastest in time(90%) and lowest(99.5%) in space.
er1shivam
5
316
count the number of consistent strings
1,684
0.819
Easy
24,349
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1163984/Python3-Brute-Force
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = 0 for i in words: f = True for j in set(i): if(j not in allowed): f = False break if(f): count += 1 return count
count-the-number-of-consistent-strings
[Python3] Brute Force
VoidCupboard
4
212
count the number of consistent strings
1,684
0.819
Easy
24,350
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1056181/Python-simple-brute-force-or-95-memory-and-time
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = 0 allowed = set(allowed) for i in words: for letter in i: if letter not in allowed: count += 1 break return len(words) - count
count-the-number-of-consistent-strings
Python simple brute force | 95% memory and time
vanigupta20024
4
440
count the number of consistent strings
1,684
0.819
Easy
24,351
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1316220/Python-3-oror-Faster-than-93oror-Easy-to-understand
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: not_allowed=0 #counts the number of not allowed words for word in words: for letter in word: if letter not in allowed: not_allowed+=1 break #this word is not allowed move to next word return len(words)-not_allowed
count-the-number-of-consistent-strings
Python 3 || Faster than 93%|| Easy to understand
ana_2kacer
2
158
count the number of consistent strings
1,684
0.819
Easy
24,352
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/969684/1-liner
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: s=set(allowed) return sum(all(c in s for c in w) for w in words)
count-the-number-of-consistent-strings
1-liner
lokeshsenthilkumar
2
252
count the number of consistent strings
1,684
0.819
Easy
24,353
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2478717/Python-simple-solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = 0 for word in words: temp = set() for i in word: temp.add(i) for i in temp: if i not in allowed: break else: count += 1 return count
count-the-number-of-consistent-strings
Python simple solution
aruj900
1
134
count the number of consistent strings
1,684
0.819
Easy
24,354
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2430112/Python-Solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: c,w,a = 0,words,set(allowed) for i in range(len(w)): if not((set(w[i]))-a): c += 1 return c
count-the-number-of-consistent-strings
Python Solution
SouravSingh49
1
60
count the number of consistent strings
1,684
0.819
Easy
24,355
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2117548/easy
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: return sum(set(allowed) >= set(i) for i in words) # set(allowed) >= set(i) all characters in i are also in allowed
count-the-number-of-consistent-strings
easy
writemeom
1
43
count the number of consistent strings
1,684
0.819
Easy
24,356
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2015631/Pythin-easy-to-read-and-understand
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: ans = 0 for word in words: ans += 1 for ch in word: if ch not in allowed: ans -= 1 break return ans
count-the-number-of-consistent-strings
Pythin easy to read and understand
sanial2001
1
118
count the number of consistent strings
1,684
0.819
Easy
24,357
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1731928/8-lines-of-Python
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed = list(allowed) count = len(words) for s in words: for c in s: if(c not in allowed): count -= 1 break return count
count-the-number-of-consistent-strings
8 lines of Python
Depender
1
179
count the number of consistent strings
1,684
0.819
Easy
24,358
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1306687/Easy-Python-Solution(98.46)
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: c=0 for i in words: for j in i: if(j not in allowed): c+=1 break return len(words)-c
count-the-number-of-consistent-strings
Easy Python Solution(98.46%)
Sneh17029
1
335
count the number of consistent strings
1,684
0.819
Easy
24,359
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1243574/Python3-easy-solution-with-90-best-time
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = 0 for word in words: for char in word: if char not in allowed: count += 1 break return len(words) - count
count-the-number-of-consistent-strings
Python3 easy solution with 90% best time
albezx0
1
75
count the number of consistent strings
1,684
0.819
Easy
24,360
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1188680/Python-list-comp-and-sets
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: return len([1 for word in words if set(word).issubset(allowed)])
count-the-number-of-consistent-strings
Python list comp and sets
fast_typer
1
196
count the number of consistent strings
1,684
0.819
Easy
24,361
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1135783/Python3-easy-to-understand.
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = 0 checked = 0 # Will be used to see if all the letters of word[i] == allowed for i in words: for j in i: if j in allowed: checked +=1 if checked == len(i): count+=1 checked=0 return count
count-the-number-of-consistent-strings
Python3 easy to understand.
lukaigrutinovic
1
200
count the number of consistent strings
1,684
0.819
Easy
24,362
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1026929/Python3-1-line
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: return sum(all(c in allowed for c in word) for word in words)
count-the-number-of-consistent-strings
[Python3] 1-line
ye15
1
74
count the number of consistent strings
1,684
0.819
Easy
24,363
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1026929/Python3-1-line
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: return sum(not set(word) - set(allowed) for word in words)
count-the-number-of-consistent-strings
[Python3] 1-line
ye15
1
74
count the number of consistent strings
1,684
0.819
Easy
24,364
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2846656/Python-or-2-Liner-or-Using-Set
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: set_allowed = set(allowed) return sum(set(word).issubset(set_allowed) for word in words)
count-the-number-of-consistent-strings
Python | 2 Liner | Using Set
pawangupta
0
1
count the number of consistent strings
1,684
0.819
Easy
24,365
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2804491/Python-Clean-Code
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: ans = 0 lookup = set(allowed) for string in words: is_allowed = True for character in string: if character not in lookup: is_allowed = False break if is_allowed: ans += 1 return ans # TC: O(N); SC: O(1)
count-the-number-of-consistent-strings
Python Clean Code
okhadeanimesh
0
3
count the number of consistent strings
1,684
0.819
Easy
24,366
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2804233/Memory-97oror-Python3
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: k=0 for i in words: x=False for j in i: if j in allowed: x=True else: x=False break if x: k+=1 return k
count-the-number-of-consistent-strings
Memory 97%|| Python3
Samandar_Abduvaliyev
0
7
count the number of consistent strings
1,684
0.819
Easy
24,367
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2803818/Python-Easy-Understand-Solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: consistent_string_count = 0 for word in words: for char in word: if char not in allowed: break else: consistent_string_count += 1 return consistent_string_count
count-the-number-of-consistent-strings
Python Easy Understand Solution
namashin
0
2
count the number of consistent strings
1,684
0.819
Easy
24,368
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2783466/Simple-Python-Solution-oror-Easy-and-Explained
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed = set(allowed) count = 0 for word in words: for letter in word: if letter not in allowed: count += 1 break return len(words) - count
count-the-number-of-consistent-strings
Simple Python Solution || Easy & Explained
dnvavinash
0
2
count the number of consistent strings
1,684
0.819
Easy
24,369
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2767671/Python-3-Using-Bit-Manipulation
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: pat = 0 for i in allowed: pat |= 1<<(ord(i)-ord("a")) ans = 0 for word in words: temp = 0 for i in word: temp |= 1<<(ord(i)-ord("a")) if temp|pat==pat: ans += 1 return ans ```
count-the-number-of-consistent-strings
[Python 3] Using Bit Manipulation
user2667O
0
3
count the number of consistent strings
1,684
0.819
Easy
24,370
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2752114/Fastest-1-line-Python-3-solution-100-faster-than-any-other-codes
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed = set(allowed) count = 0 for word in words: for letter in word: if letter not in allowed: count += 1 break return len(words) - count
count-the-number-of-consistent-strings
Fastest 1 line Python 3 solution 100 faster than any other codes
avs-abhishek123
0
4
count the number of consistent strings
1,684
0.819
Easy
24,371
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2744865/O(n)-or-Python-or-2-pointer-approach
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: not_allowed_word_count = 0 w, c = 0, 0 while w < len(words): if words[w][c] not in allowed: not_allowed_word_count += 1 w += 1 c = 0 continue if c == len(words[w]) - 1: w += 1 c = 0 continue c += 1 return len(words) - not_allowed_word_count
count-the-number-of-consistent-strings
O(n) | Python | 2 pointer approach
kawamataryo
0
4
count the number of consistent strings
1,684
0.819
Easy
24,372
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2730477/Python3-Solution-beats-98.1
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count_consistent = len(words) for word in words: for w in word: if w not in allowed: count_consistent -= 1 break return count_consistent
count-the-number-of-consistent-strings
Python3 Solution - beats 98.1%
sipi09
0
2
count the number of consistent strings
1,684
0.819
Easy
24,373
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2547168/EASY-PYTHON3-SOLUTION
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: res=0 for i in words: if (w := set(i)) &amp; set(allowed) == w: #If the set of "i" is equal to set of "allowed", + 1 to the output res+=1 return (res)
count-the-number-of-consistent-strings
🔥 EASY PYTHON3 SOLUTION 🔥
rajukommula
0
42
count the number of consistent strings
1,684
0.819
Easy
24,374
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2509787/Python-or-One-liner-or-Intersection
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: return sum([1 for word in words if (w := set(word)) &amp; set(allowed) == w])
count-the-number-of-consistent-strings
Python | One-liner | Intersection
Wartem
0
65
count the number of consistent strings
1,684
0.819
Easy
24,375
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2415520/Easy-Python3-or-A.issubset(B)
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed = set(allowed) count = 0 for el in words: if set(el).issubset(allowed) == True: count += 1 return count
count-the-number-of-consistent-strings
Easy Python3 | A.issubset(B)
Sergei_Gusev
0
22
count the number of consistent strings
1,684
0.819
Easy
24,376
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2408685/1684.-Count-the-Number-of-Consistent-Strings%3A-One-Liner
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: return sum([1 for element in words if set(element).issubset(allowed)])
count-the-number-of-consistent-strings
1684. Count the Number of Consistent Strings: One Liner
rogerfvieira
0
27
count the number of consistent strings
1,684
0.819
Easy
24,377
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2381183/Memory-Usage-less-than-97.93-and-faster-than-74.20
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = 0 for word in words: leftover = set(word) - set(allowed) if len(leftover) == 0: count += 1 return count
count-the-number-of-consistent-strings
Memory Usage less than 97.93% and faster than 74.20%
samanehghafouri
0
43
count the number of consistent strings
1,684
0.819
Easy
24,378
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2342797/python3-solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: res = 0 for word in words: for char in set(word): if char not in allowed: res += 1 break return len(words) - res
count-the-number-of-consistent-strings
python3 solution
jd_mahmud
0
58
count the number of consistent strings
1,684
0.819
Easy
24,379
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2326330/Python3-Solution-with-using-hashset
class Solution: def _helper(self, word, allowed_set): for char in word: if char not in allowed_set: return False return True def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed_set = set(allowed) res = 0 for word in words: if self._helper(word, allowed_set): res += 1 return res
count-the-number-of-consistent-strings
[Python3] Solution with using hashset
maosipov11
0
22
count the number of consistent strings
1,684
0.819
Easy
24,380
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2180558/Python-Solution-Easy-to-Understand
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: c=0 for i in range(len(words)): for j in range(len(allowed)): if allowed[j] in words[i]: words[i]=words[i].replace(allowed[j],"") if len(words[i])==0: c+=1 break return c
count-the-number-of-consistent-strings
Python Solution - Easy to Understand
T1n1_B0x1
0
39
count the number of consistent strings
1,684
0.819
Easy
24,381
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2123145/Easy-python-solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: c = 0 at = Counter(allowed) for word in words: ct = Counter(word) if set(list(ct.keys())).issubset(set(list(at.keys()))): c += 1 return c
count-the-number-of-consistent-strings
Easy python solution
nikhitamore
0
101
count the number of consistent strings
1,684
0.819
Easy
24,382
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2121826/Python-simple-solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: ans = 0 for word in words: for letter in word: if letter not in allowed: break else: ans += 1 return ans
count-the-number-of-consistent-strings
Python simple solution
StikS32
0
74
count the number of consistent strings
1,684
0.819
Easy
24,383
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2068683/Python3-SImple-Beats-99
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed_set = set(allowed) count = 0 for word in words: flag = True for letter in word: if letter not in allowed_set: flag = False break if flag: count+=1 return count Runtime: 212 ms, faster than 99.45% of Python3 online submissions for Count the Number of Consistent Strings. Memory Usage: 16 MB, less than 89.80% of Python3 online submissions for Count the Number of Consistent Strings.
count-the-number-of-consistent-strings
Python3 SImple Beats 99%
emerald19
0
99
count the number of consistent strings
1,684
0.819
Easy
24,384
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2027204/Python-Clean-and-Concise!
class Solution: def countConsistentStrings(self, allowed, words): allowed = set(allowed) return sum(all(c in allowed for c in set(word)) for word in words)
count-the-number-of-consistent-strings
Python - Clean and Concise!
domthedeveloper
0
85
count the number of consistent strings
1,684
0.819
Easy
24,385
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/2014246/Python-Solution-or-Using-Set-Intersection
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed = set(allowed) count = 0 for word in words: intsct = set(word).intersection(allowed) if set(word) == intsct: count += 1 return count
count-the-number-of-consistent-strings
Python Solution | Using Set Intersection
ssshekhu53
0
49
count the number of consistent strings
1,684
0.819
Easy
24,386
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1891284/Python3-One-Loop-Using-Set-and-Sorted-Solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: res = 0 allowed = "".join(sorted(allowed)) for k in words: if allowed == "".join(sorted(set(k+allowed))): res += 1 else: return res
count-the-number-of-consistent-strings
✔Python3 One Loop Using Set and Sorted Solution
khRay13
0
96
count the number of consistent strings
1,684
0.819
Easy
24,387
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1865886/Python-solution-using-sets
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed_set = set(allowed) count = 0 for i in words: if not(set(i).difference(allowed_set)): count += 1 return count
count-the-number-of-consistent-strings
Python solution using sets
alishak1999
0
119
count the number of consistent strings
1,684
0.819
Easy
24,388
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1839407/Easy-Python-Solution-or-Faster-than-77.84
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: a=set(list(allowed)) ans=0 for i in words: t=set(list(i)) if t.issubset(a): ans+=1 return ans ```
count-the-number-of-consistent-strings
Easy Python Solution | Faster than 77.84 %
manav023
0
106
count the number of consistent strings
1,684
0.819
Easy
24,389
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1801210/Easy-Python-Loop
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: c=0 for i in words: for j in set(i): if j not in allowed: c+=1 # print(c) break return len(words)-c
count-the-number-of-consistent-strings
Easy Python Loop
adityabaner
0
113
count the number of consistent strings
1,684
0.819
Easy
24,390
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1654662/Runtime%3A-512-ms-faster-than-5.06-of-Python3-online-submissions
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: length = 0 for i in words: str1 = "" for j in range(len(i)): if i[j] in allowed: str1 += i[j] if len(i) == len(str1): length += 1 return length
count-the-number-of-consistent-strings
Runtime: 512 ms, faster than 5.06% of Python3 online submissions
piyushkumarpiyushkumar6
0
29
count the number of consistent strings
1,684
0.819
Easy
24,391
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1561404/Easy-Python3-solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: allowed_set: set = set(list(allowed)) consistent: int = 0 for word in words: for char in word: if char not in allowed_set: break else: consistent += 1 return consistent
count-the-number-of-consistent-strings
Easy Python3 solution
sirenescx
0
144
count the number of consistent strings
1,684
0.819
Easy
24,392
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1556335/Python-Easy-Solution-or-Faster-than-98
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count = len(words) for word in words: for w in word: if w not in allowed: count -= 1 break return count
count-the-number-of-consistent-strings
Python Easy Solution | Faster than 98%
leet_satyam
0
142
count the number of consistent strings
1,684
0.819
Easy
24,393
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1355273/python-3-solution
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: count=0 for i in range (0,len(words)): for j in range(0,len(words[i])): if words[i][j] not in allowed: count=count+1 break return len(words)-count
count-the-number-of-consistent-strings
python 3 solution
minato_namikaze
0
99
count the number of consistent strings
1,684
0.819
Easy
24,394
https://leetcode.com/problems/count-the-number-of-consistent-strings/discuss/1348465/Python-3-oror-Using-Single-For-Loop
class Solution: def countConsistentStrings(self, allowed: str, words: List[str]) -> int: consistentStringCount = 0 setAllowed = set(allowed) for word in words: setWord = set(word) if len(setWord - setAllowed) == 0: consistentStringCount +=1 return consistentStringCount
count-the-number-of-consistent-strings
Python 3 || Using Single For Loop
SP_Roger
0
87
count the number of consistent strings
1,684
0.819
Easy
24,395
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/1439047/Python-3-or-O(N)-Prefix-Sum-Clean-or-Explanation
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: pre_sum = [0] for num in nums: # calculate prefix sum pre_sum.append(pre_sum[-1] + num) n = len(nums) # render the output return [(num*(i+1) - pre_sum[i+1]) + (pre_sum[-1]-pre_sum[i] - (n-i)*num) for i, num in enumerate(nums)]
sum-of-absolute-differences-in-a-sorted-array
Python 3 | O(N), Prefix Sum, Clean | Explanation
idontknoooo
3
327
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,396
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/2821092/O(n)-time-complexity-beats-97.5-python
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: total_sum = sum(nums) N = len(nums) ans = [] l_sum = 0 l_covered = 0 for i in range(N): r_size = N-i-1 r_sum = total_sum - l_sum - nums[i] s = l_covered*nums[i] - l_sum + r_sum - r_size*nums[i] # print(i, l_covered*nums[i] - l_sum, r_sum - r_size*nums[i]) ans.append(s) l_sum += nums[i] l_covered += 1 return ans
sum-of-absolute-differences-in-a-sorted-array
O(n) time complexity, beats 97.5%, python
Rohit_Annamaneni
0
2
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,397
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/2758888/Python-3%3A-Linear-time-complexity
class Solution: def getSumAbsoluteDifferences(self, nums: List[int]) -> List[int]: length = len(nums) output = length*[0] pos= sum([abs(item - nums[0]) for item in nums]) neg = 0 output[0] = pos for i in range(1,length): diff = nums[i] - nums[i-1] neg += diff*i pos -= diff*(length-i) output[i] = pos + neg return output return summand_lst
sum-of-absolute-differences-in-a-sorted-array
Python 3: Linear time complexity
cleon082
0
12
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,398
https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/discuss/2738667/Simple-and-Easy-to-Understand-or-O(n)-Time-Complexity-or-Python
class Solution(object): def getSumAbsoluteDifferences(self, nums): lSum, rSum, l, r = 0, sum(nums), 0, len(nums) - 1 ans = [] for n in nums: rSum -= n ans.append((n * l) - lSum + rSum - (n * r)) lSum += n l += 1 r -= 1 return ans
sum-of-absolute-differences-in-a-sorted-array
Simple and Easy to Understand | O(n) Time Complexity | Python
its_krish_here
0
22
sum of absolute differences in a sorted array
1,685
0.649
Medium
24,399