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/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1071515/Python-greater-4-lines
class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: for idx, word in enumerate(sentence.split()): if searchWord == word[:len(searchWord)]: return idx+1 return -1
check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
Python -> 4 lines
dev-josh
0
74
check if a word occurs as a prefix of any word in a sentence
1,455
0.642
Easy
21,700
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1057113/Python3-simple-solution
class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: ind = -1 sentence = sentence.split(' ') for index,word in enumerate(sentence): if word.startswith(searchWord): return (index+1) return ind
check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
Python3 simple solution
EklavyaJoshi
0
39
check if a word occurs as a prefix of any word in a sentence
1,455
0.642
Easy
21,701
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/1047030/Easy-python-3-solution
class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: for i, el in enumerate(sentence.split(' '), 1): if el.startswith(searchWord): return i return -1
check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
Easy python 3 solution
ctarriba9
0
36
check if a word occurs as a prefix of any word in a sentence
1,455
0.642
Easy
21,702
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/859948/Python3-1-line
class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: return next((i for i, word in enumerate(sentence.split(), 1) if word.startswith(searchWord)), -1)
check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
[Python3] 1-line
ye15
0
45
check if a word occurs as a prefix of any word in a sentence
1,455
0.642
Easy
21,703
https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/discuss/859948/Python3-1-line
class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: for i, word in enumerate(sentence.split()): if word.startswith(searchWord): return i+1 return -1
check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence
[Python3] 1-line
ye15
0
45
check if a word occurs as a prefix of any word in a sentence
1,455
0.642
Easy
21,704
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/1504290/Python3-simple-soluton
class Solution: def maxVowels(self, s: str, k: int) -> int: x = 0 for i in range(k): if s[i] in ('a', 'e', 'i', 'o', 'u'): x += 1 ans = x for i in range(k,len(s)): if s[i] in ('a', 'e', 'i', 'o', 'u'): x += 1 if s[i-k] in ('a', 'e', 'i', 'o', 'u'): x -= 1 ans = max(ans,x) return ans
maximum-number-of-vowels-in-a-substring-of-given-length
Python3 simple soluton
EklavyaJoshi
2
89
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,705
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/1485108/Brute-Force-to-Optimal-Approach-greatergreater-Thought-Process
class Solution: def maxVowels(self, s: str, k: int) -> int: n = len(s) #STEP 1 arr = [] for i in range(0, n): arr.append(s[i : i + k]) vowels = ["a", "e", "i", "o", "u"] arr1 = [] #STEP 2 for i in arr: count = 0 if len(i) == k: for j in i: if j in vowels: count += 1 arr1.append(count) #STEP 3 if len(arr1) != 0: value = max(arr1) return value else: return 0
maximum-number-of-vowels-in-a-substring-of-given-length
Brute Force to Optimal Approach -->> Thought Process
aarushsharmaa
2
103
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,706
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/1485108/Brute-Force-to-Optimal-Approach-greatergreater-Thought-Process
class Solution: def maxVowels(self, s: str, k: int) -> int: n = len(s) vowels = { 'a', 'e', 'i', 'o', 'u' } count = 0 # Step 1 : We will find number of vowels in the first substring of length k : from 0th index till (k-1)th index for i in range(0, k): if s[i] in vowels: count += 1 # record for maximum vowel count in substring max_vowel_count = count # sliding window of size k # starts from k and window from [0, k-1] inclusive is already considered for tail_index in range(k, n): head_index = tail_index - k head_char, tail_char = s[head_index], s[tail_index] if head_char in vowels: count -= 1 if tail_char in vowels: count += 1 max_vowel_count = max(max_vowel_count, count) return max_vowel_count
maximum-number-of-vowels-in-a-substring-of-given-length
Brute Force to Optimal Approach -->> Thought Process
aarushsharmaa
2
103
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,707
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2840326/Python-prefix-sum
class Solution: def maxVowels(self, s: str, k: int) -> int: prefix_sum = [] if s[0] in "aeiou": prefix_sum.append(1) else: prefix_sum.append(0) for i in range(1, len(s)): if s[i] in "aeiou": prefix_sum.append(prefix_sum[-1] + 1) else: prefix_sum.append(prefix_sum[-1]) result = prefix_sum[k - 1] for i in range(k, len(s)): result = max(result, prefix_sum[i] - prefix_sum[i - k]) return result
maximum-number-of-vowels-in-a-substring-of-given-length
Python prefix sum
Bread0307
0
3
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,708
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2780134/100-Faster-or-Two-different-Solutions-or-O(n)-Time-and-O(1)-Space-or-Python
class Solution(object): def maxVowels(self, s, k): ans = ptr = count = 0 for i in range(len(s)): if k > i: if s[i] in 'aeiou': count += 1 ans = count else: if s[ptr] in 'aeiou': count -= 1 if s[i] in 'aeiou': count += 1 if count > ans: ans = count ptr += 1 return ans
maximum-number-of-vowels-in-a-substring-of-given-length
100% Faster | Two different Solutions | O(n) Time and O(1) Space | Python
its_krish_here
0
6
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,709
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2780134/100-Faster-or-Two-different-Solutions-or-O(n)-Time-and-O(1)-Space-or-Python
class Solution(object): def maxVowels(self, s, k): arr = [0] * len(s) ans = 0 for i in range(len(s)): if s[i] == 'a' or s[i] == 'o' or s[i] == 'e' or s[i] == 'u' or s[i] == 'i': arr[i] = 1 sum_ = 0 ptr = 0 for i in range(len(arr)): if k > i: sum_ += arr[i] ans = sum_ else: sum_ -= arr[ptr] sum_ += arr[i] if sum_ > ans: ans = sum_ ptr += 1 return ans
maximum-number-of-vowels-in-a-substring-of-given-length
100% Faster | Two different Solutions | O(n) Time and O(1) Space | Python
its_krish_here
0
6
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,710
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2766848/Sliding-Window-Solution-in-O(n)
class Solution: def maxVowels(self, s: str, k: int) -> int: return self.slidingwindow(s,k) def slidingwindow(self,s,k): v=['a','e','i','o','u'] maxi,currmax=0,0 for c,e in enumerate(s): if e in v: currmax+=1 if (c-k)>=0 and s[c-k] in v: currmax-=1 maxi=max(currmax,maxi) return maxi
maximum-number-of-vowels-in-a-substring-of-given-length
Sliding Window Solution in O(n)
pjvkumar999
0
5
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,711
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2766846/Sliding-Window-Solution-in-O(n)
class Solution: def maxVowels(self, s: str, k: int) -> int: return self.slidingwindow(s,k) def slidingwindow(self,s,k): v=['a','e','i','o','u'] maxi,currmax=0,0 for c,e in enumerate(s): if e in v: currmax+=1 if (c-k)>=0 and s[c-k] in v: currmax-=1 maxi=max(currmax,maxi) return maxi
maximum-number-of-vowels-in-a-substring-of-given-length
Sliding Window Solution in O(n)
pjvkumar999
0
2
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,712
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2761292/Python-3-Solution
class Solution: def maxVowels(self, s: str, k: int) -> int: V = set(["a","e","i","o","u"]) arr = [1 if x in V else 0 for x in s] l, r = 0, k suma = sum(arr[0:k]) res = suma while r<len(s): suma += (arr[r]-arr[l]) r+=1 l+=1 res = max(suma,res) return res
maximum-number-of-vowels-in-a-substring-of-given-length
Python 3 Solution
mati44
0
5
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,713
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2645939/Python3-Solution-oror-O(N)-Time-and-O(1)-Space-Complexity
class Solution: def maxVowels(self, s: str, k: int) -> int: vowels={"a","e","i","o","u"} n=len(s) countVowel=0 for i in range(k): if s[i] in vowels: countVowel+=1 maxVowelCount=countVowel for i in range(k,n): print(countVowel) if maxVowelCount==k: return k if s[i-k] in vowels: countVowel-=1 if s[i] in vowels: countVowel+=1 if countVowel>maxVowelCount: maxVowelCount=countVowel return maxVowelCount
maximum-number-of-vowels-in-a-substring-of-given-length
Python3 Solution || O(N) Time & O(1) Space Complexity
akshatkhanna37
0
5
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,714
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2612215/Python-Sliding-window-approch
class Solution: def maxVowels(self, s: str, k: int) -> int: left = 0 right = 0 res = 0 v = 0 #to count our vowels while looping vowel = 'aeiou' for r in range(len(s)): if s[r] in vowel: # if the current value is vowel increment v by 1 v+=1 if (r-l + 1) == k: # if the length of the left and the right are equal to k then find the max of the counted vowels until now res = max(res, v) if s[l] in vowel: # if the left value is vowel we will decrement v by one v -=1 l+=1 return res
maximum-number-of-vowels-in-a-substring-of-given-length
Python Sliding window approch
pandish
0
78
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,715
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2608341/easy-python-solution
class Solution: def maxVowels(self, s: str, k: int) -> int: letterList = [i for i in s] maxCount = 0 vowels = ['a', 'e', 'i', 'o', 'u'] if k == 1 : for i in vowels : if i in letterList : return 1 else : for i in range(len(letterList)-k+1) : if i == 0 : toCheck = letterList[i:i+k] totalVowelCount = toCheck.count('a') + toCheck.count('e') + toCheck.count('i') + toCheck.count('o') + toCheck.count('u') else : if letterList[i-1] in vowels : totalVowelCount -= 1 if (letterList[i+k-1] in vowels) : totalVowelCount += 1 if totalVowelCount > maxCount : maxCount = totalVowelCount return maxCount
maximum-number-of-vowels-in-a-substring-of-given-length
easy python solution
sghorai
0
13
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,716
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2583416/Clean-Sliding-window-python3-solution
class Solution: # O(n-k) time, # O(1) space, # Approach: sliding window, two pointers, hashtable, def maxVowels(self, s: str, k: int) -> int: n = len(s) l, r = 0, k count = Counter(s[l:r]) def countVowels(count) -> int: vowels = ['a', 'e', 'i', 'o', 'u'] tot = 0 for vowel in vowels: tot += count.get(vowel, 0) return tot max_count = countVowels(count) while r < n: count[s[l]] -=1 count[s[r]] = count.get(s[r], 0) + 1 max_count = max(max_count, countVowels(count)) if max_count == k: return max_count l +=1 r +=1 return max_count
maximum-number-of-vowels-in-a-substring-of-given-length
Clean Sliding window python3 solution
destifo
0
9
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,717
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/2534466/Python-Sliding-Window
class Solution: def maxVowels(self, s: str, k: int) -> int: _set=('a', 'e', 'i', 'o','u') _max,count,i=0,0,0 for j in range(len(s)): if s[j] in _set: count+=1 k-=1 if k==0: _max=max(_max,count) if s[i] in _set: count-=1 i+=1 k=1 return _max
maximum-number-of-vowels-in-a-substring-of-given-length
Python Sliding Window
omarihab99
0
17
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,718
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/1989466/Easy-Python-or-99-Speed-or-O(1)-space-O(n)-time
class Solution: def maxVowels(self, A, k): vocals = set('aeiou') n = 0 record = 0 for i,x in enumerate(A): if x in vocals: n += 1 if (i-k)>=0 and (A[i-k] in vocals): n -= 1 if n>record: record = n return record
maximum-number-of-vowels-in-a-substring-of-given-length
Easy Python | 99% Speed | O(1) space, O(n) time
Aragorn_
0
67
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,719
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/1953214/easy-to-understand-python-solution
class Solution(object): def maxVowels(self, s, k): cx = 0 mx = 0 i = 0 for j in range(len(s)): if s[j] in ["a","e","i","o","u"]: cx+=1 if j-i+1 == k: mx = max(mx,cx) if s[i] in ["a","e","i","o","u"]: cx-=1 i+=1 return mx
maximum-number-of-vowels-in-a-substring-of-given-length
easy to understand python solution
bhuvanm424
0
45
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,720
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/1711724/Python3-Sliding-Window-or-Easy-Solution
class Solution: def maxVowels(self, s: str, k: int) -> int: j,cnt=0,0 vowels="aeiou" for j in range(k): if s[j] in vowels: cnt+=1 max_cnt=cnt for j in range(k,len(s)): if s[j-k] in vowels: cnt-=1 if s[j] in vowels: cnt+=1 max_cnt=max(max_cnt,cnt) return(max_cnt)
maximum-number-of-vowels-in-a-substring-of-given-length
Python3 Sliding Window | Easy Solution
shandilayasujay
0
95
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,721
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/648936/PYTHON-Using-Kadane's-Aglorithm-288ms
class Solution: def maxVowels(self, s: str, k: int) -> int: word_list =list(s) vowel = {'a', 'e', 'i', 'o', 'u'} for i in range(len(word_list)): if vowel.intersection({word_list[i]}): word_list[i] = 1 else: word_list[i] = 0 max_sum = 0 for i in range(k): max_sum += word_list[i] curr_sum = max_sum for i in range(k, len(word_list)): curr_sum += word_list[i] - word_list[i - k] max_sum = max(max_sum, curr_sum) return max_sum
maximum-number-of-vowels-in-a-substring-of-given-length
[PYTHON] Using Kadane's Aglorithm - 288ms
amanpathak2909
0
51
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,722
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/648936/PYTHON-Using-Kadane's-Aglorithm-288ms
class Solution: def maxVowels2(self, s: str, k: int) -> int: max_vowel = 0 vowel = {'a', 'e', 'i', 'o', 'u'} word_list =list(s) for i in range(len(word_list)): if vowel.intersection({word_list[i]}): word_list[i] = 1 else: word_list[i] = 0 for all_windows in range(len(word_list) - k+1): current_vowels = sum(word_list[all_windows:all_windows + k]) if current_vowels > max_vowel: max_vowel = current_vowels return max_vowel
maximum-number-of-vowels-in-a-substring-of-given-length
[PYTHON] Using Kadane's Aglorithm - 288ms
amanpathak2909
0
51
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,723
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/discuss/648936/PYTHON-Using-Kadane's-Aglorithm-288ms
class Solution: def maxVowels1(self, s: str, k: int) -> int: vowel = {'a','e','i','o','u'} maxVowels = 0 for all_windows in range(len(s) - k+1): currentVowels = 0 for letter in s[all_windows:all_windows + k]: if vowel.intersection({letter}): currentVowels+=1 if currentVowels > maxVowels: maxVowels= currentVowels return maxVowels
maximum-number-of-vowels-in-a-substring-of-given-length
[PYTHON] Using Kadane's Aglorithm - 288ms
amanpathak2909
0
51
maximum number of vowels in a substring of given length
1,456
0.581
Medium
21,724
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573237/LeetCode-The-Hard-Way-Explained-Line-By-Line
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode], cnt = 0) -> int: if not root: return 0 cnt ^= 1 << (root.val - 1) if root.left is None and root.right is None: return 1 if cnt &amp; (cnt - 1) == 0 else 0 return self.pseudoPalindromicPaths(root.left, cnt) + self.pseudoPalindromicPaths(root.right, cnt)
pseudo-palindromic-paths-in-a-binary-tree
🔥 [LeetCode The Hard Way] 🔥 Explained Line By Line
wingkwong
68
2,600
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,725
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2576551/EASY-oror-JAVA-oror-C%2B%2B-oror-PYTHON-oror-EXPLAINED-WITH-VIDEO-oror-BEGINNER-FRIENDLY
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: ## Function to check if current path has a permutation that's a palindrome def isPalindromicPath(palinArr: [int]) -> bool: hasSeenFirstOdd: bool = False for i in range(0, len(palinArr)): if(palinArr[i] % 2 == 1): if hasSeenFirstOdd: return False hasSeenFirstOdd = True return True ## Wrapper for function that calculates the number of pseudo palindromic paths def calcPalindromicPaths(root: Optional[TreeNode], arr: [int]) -> int: count: int = 0 if root == None: return 0 arr[root.val] += 1 ## Leaf Node: No children if(root.left == None and root.right == None): if(isPalindromicPath(arr)): count = 1 if(root.left != None): count += calcPalindromicPaths(root.left, arr) if(root.right != None): count += calcPalindromicPaths(root.right, arr) arr[root.val] -= 1 return count; dparr: [int] = [0] * 10 return calcPalindromicPaths(root, dparr)
pseudo-palindromic-paths-in-a-binary-tree
🥇 EASY || JAVA || C++ || PYTHON || EXPLAINED WITH VIDEO || BEGINNER FRIENDLY 🔥🔥
AlgoLock
8
189
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,726
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573162/Preorder-Traversal-python-solution-using-mapping
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: def pre(root,dic): if root is None: return 0 if root.left is None and root.right is None: if root.val not in dic: dic[root.val]=1 else: dic[root.val]+=1 fg=0 for i in dic: if dic[i]%2==1 and fg==1: return 0 if dic[i]%2==1: fg=1 return 1 if root.val not in dic: dic[root.val]=1 else: dic[root.val]+=1 x=pre(root.left,dic) if root.left: dic[root.left.val]-=1 y=pre(root.right,dic) if root.right: dic[root.right.val]-=1 return x+y dic={} return pre(root,dic)
pseudo-palindromic-paths-in-a-binary-tree
Preorder Traversal python solution using mapping
shubham_1307
3
259
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,727
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573491/Python-Solution-With-DefaultDict
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: self.c=0 """Function to check if any permutation of self.d in pallindromic""" def ispallindrome(d): k=0 for i in d.values(): if i%2==1: k+=1 if k>1: return False return True def dfs(root,d): if root: d[root.val]+=1 if not root.left and not root.right: if ispallindrome(d): self.c+=1 dfs(root.left,d) dfs(root.right,d) d[root.val]-=1 return dfs(root,defaultdict(int)) return self.c
pseudo-palindromic-paths-in-a-binary-tree
Python Solution With DefaultDict
a_dityamishra
1
46
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,728
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573127/Python-DFS-Iterative-approach-O(N)-TC-and-SC
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: answer = 0 val_count = [0] * 9 visited = {} stack = [root] while stack != []: node = stack.pop() if visited.get(node) is None: val_count[node.val-1] += 1 stack.append(node) visited[node] = 1 is_leaf = True if node.right: is_leaf = False stack.append(node.right) if node.left: is_leaf = False stack.append(node.left) if is_leaf: odds = 0 for i in val_count: if i % 2 == 1: odds += 1 answer += 1 if odds <= 1 else 0 else: val_count[node.val-1] -= 1 return answer
pseudo-palindromic-paths-in-a-binary-tree
Python DFS Iterative approach O(N) TC and SC
ChristianK
1
97
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,729
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2576784/Very-easy-Python-%3A
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: def path(root,sett): if not root: return 0 if root.val in sett: sett.remove(root.val) else: sett.add(root.val) if not root.left and not root.right: if len(sett)<=1: return 1 return 0 return path(root.left,set(sett))+path(root.right,set(sett)) return path(root,set())
pseudo-palindromic-paths-in-a-binary-tree
Very easy Python :
goxy_coder
0
10
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,730
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2575018/Simple-%22python%22-Solution
class Solution: def pseudoPalindromicPaths(self, root: TreeNode) -> int: count = 0 # root = [2,3,1,3,1,null,1] stack = [(root, 0)] # path = 00000000 # nade = 2 # 1 << node.val = 1 << 2 = 00000100 # path = 00000100 # node = 3 # 1 << node.val = 00001000 # path = 00001100 # node =3 # 1 << node.val = 00001000 # path = 00000100 # path &amp; (path -1) # path -1 = 00000011 # count =1 while stack: node, path = stack.pop() if node is not None: path = path ^ (1 << node.val) # if it's a leaf, check if the path is pseudo-palindromic if node.left is None and node.right is None: # check if at most one digit has an odd frequency if path &amp; (path - 1) == 0: count += 1 else: stack.append((node.left, path)) stack.append((node.right, path)) return count
pseudo-palindromic-paths-in-a-binary-tree
Simple "python" Solution
anandchauhan8791
0
24
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,731
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2574637/Python-Simple-Solution
class Solution: output = 0 def traversal(self, node, pairs): p = pairs.copy() if node.val in p: p.remove(node.val) else: p.add(node.val) if node.left: self.traversal(node.left, p) if node.right: self.traversal(node.right, p) if not node.left and not node.right: if len(p) == 1 or len(p) == 0: self.output += 1 def pseudoPalindromicPaths(self,root): self.traversal(root, set()) return self.output
pseudo-palindromic-paths-in-a-binary-tree
Python Simple Solution
AxelDovskog
0
16
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,732
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2574552/Python3-DFS-TC-O(n)-SC-O(h)
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: def dfs(node: Optional[TreeNode], counter: list[int]) -> int: counter[node.val-1] = 0 if counter[node.val-1] else 1 if not (node.left or node.right): # it is a leaf ret = 0 if sum(counter) > 1 else 1 else: ret = 0 if node.left: ret = dfs(node.left, counter) if node.right: ret += dfs(node.right, counter) counter[node.val-1] = 0 if counter[node.val-1] else 1 return ret return dfs(root, [0] * 9)
pseudo-palindromic-paths-in-a-binary-tree
[Python3] DFS TC = O(n), SC = O(h)
geka32
0
11
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,733
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2574527/Python3-Solution-or-DFS-or-O(n)
class Solution: def pseudoPalindromicPaths (self, root): digits = [0] * 10 def dfs(node): if not node: return 0 digits[node.val] += 1 ans = dfs(node.left) + dfs(node.right) if node.left or node.right else int(sum(i&amp;1 for i in digits) <= 1) digits[node.val] -= 1 return ans return dfs(root)
pseudo-palindromic-paths-in-a-binary-tree
✔ Python3 Solution | DFS | O(n)
satyam2001
0
5
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,734
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2574369/Python-BitMask-Stack
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: ans = 0 def isPalindromic(mask): """ When we let mask turn into binary like: 10010 it means value 1' occurrence is odd time because if the digit occurrence even time, the position should be 0 We just have to count how many digit' occurrence is odd if odd_count smaller than 1, it incidates that we could make the digit become Palindromic after rearranged """ odd_count = 0 for i in range(1, 10): if mask &amp; (1 << i): odd_count += 1 return odd_count <= 1 # current node, mask stack = [(root, 0)] while stack: current, mask = stack.pop() # update the mask # e.g. # current mask is 10010 (in binary) # current value is 1 # new_mask should be 10000 # therefore, we can use xor to create new mask new_mask = mask ^ (1 << current.val) if not current.left and not current.right: ans += isPalindromic(new_mask) continue if current.left: stack.append((current.left, new_mask)) if current.right: stack.append((current.right, new_mask)) return ans
pseudo-palindromic-paths-in-a-binary-tree
Python BitMask, Stack
TerryHung
0
5
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,735
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2574270/Simple-Python-Solution-well-explained
class Solution: # Keep rolling down the binary tree and use bitmasking to keep a store of seen elements # Since data values are restricted to be between 0 <= x <= 9 we can easily maintain a mask # We use the mask as a toggle or like a switch and we have to find the number of turned on switches # If it is 1(number of on bits) then we can count it as a possible answer. # Why? because see this eg - 1 2222 33 4444 -> can be made into a palindrome by keeping 1 at the center! def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: count = 0 def finder(curr, mask): nonlocal count if curr.left: finder(curr.left, mask ^ (1 << curr.val)) if curr.right: finder(curr.right, mask ^ ( 1 << curr.val)) if not curr.right and not curr.left: mask ^= (1 << curr.val) if mask &amp; (mask - 1) == 0: count += 1 # checking number of 1s == 1 finder(root, 0) return count
pseudo-palindromic-paths-in-a-binary-tree
Simple Python Solution well explained
shiv-codes
0
12
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,736
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573539/Easy-Python-and-C%2B%2B-solution-using-DFS
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: if not root: return 0 #If tree is empty if root and not root.left and not root.right: return 1 #if Tree contains only root node self.ans = 0 self.d = dict() #Dictionary to store frequency of node values def dfs(node): if node: self.d[node.val] = self.d.get(node.val,0)+1 dfs(node.left) dfs(node.right) if not node.left and not node.right: #If we are at leaf node then check for palindrome flag = True for k,v in self.d.items(): if v%2==1: if flag: flag = False else: break else: #print(self.d) self.ans+=1 self.d[node.val]-=1 # removing count of the current node return dfs(root) return self.ans
pseudo-palindromic-paths-in-a-binary-tree
Easy Python and C++ solution using DFS
Akash3502
0
8
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,737
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573414/Easy-DFS-%2B-freq-array-TC-O(n)
class Solution: def solve(self,root,freq): if root is None: return 0 if root.left is None and root.right is None: freq[root.val]+=1 c = 0 for i in range(1,10): if freq[i]%2!=0: c+=1 if c>1: freq[root.val]-=1 return 0 freq[root.val]-=1 return 1 freq[root.val]+=1 left = self.solve(root.left,freq) right = self.solve(root.right,freq) freq[root.val]-=1 return left+right def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: freq = [0]*10 return self.solve(root,freq)
pseudo-palindromic-paths-in-a-binary-tree
Easy DFS + freq array TC-O(n)
Kakashi_97
0
11
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,738
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573353/python3-bit-masking-sol-for-reference
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: def n(node, mask): if not node: return 0 if not node.left and not node.right: mask = mask ^ (1 << node.val) return 1 if (mask &amp; (mask-1) == 0) else 0 return n(node.left, mask ^ (1 << node.val)) + n(node.right, mask ^ (1 << node.val)) return n(root, 0)
pseudo-palindromic-paths-in-a-binary-tree
[python3] bit masking sol for reference
vadhri_venkat
0
9
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,739
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573216/Python-dfs-with-hashset
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: self.ans = 0 self.dfs(root, set()) return self.ans def dfs(self, root, path): if root.val in path: path.remove(root.val) else: path.add(root.val) if root.left: self.dfs(root.left, path) if root.right: self.dfs(root.right, path) if not root.left and not root.right: if len(path)<=1: self.ans+=1 if root.val in path: path.remove(root.val) else: path.add(root.val)
pseudo-palindromic-paths-in-a-binary-tree
Python dfs with hashset
li87o
0
24
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,740
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573164/Python-DFS-(Store-digit-frequency-in-list)
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: ''' 1. For each path, collect the digits with dfs. 2. Check if the digits of each path can be a palindrome. a. Count all digits, there should only be at most odd occurrences of 1 digit b. Approach to save all numbers individually in a list is too memory consuming, so use a list of size 9 to store the frequency instead (Eg. num_list[n-1] = Number of digit n in the path) ''' self.pp_paths = 0 def dfs(curr_node, num_list): if not curr_node.left and not curr_node.right: count_odd = 0 for i in num_list: if i%2 == 1: count_odd += 1 if count_odd <= 1: self.pp_paths += 1 else: if curr_node.left: left_list = num_list.copy() left_list[curr_node.left.val-1] += 1 dfs(curr_node.left, left_list) if curr_node.right: right_list = num_list.copy() right_list[curr_node.right.val-1] += 1 dfs(curr_node.right, right_list) init_list = [0]*9 init_list[root.val-1] += 1 dfs(root,init_list) return self.pp_paths
pseudo-palindromic-paths-in-a-binary-tree
Python DFS (Store digit frequency in list)
chkmcnugget
0
8
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,741
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573156/Clean-Fast-Python3-Explained-or-DFS-and-Counter-or-O(n)
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: freqs = [0] * 10 def dfs(cur): if not cur: return 0 freqs[cur.val] += 1 # record this value if not cur.left and not cur.right: # leaf odd = False for f in freqs: if f % 2: if odd: freqs[cur.val] -= 1 return 0 odd = True freqs[cur.val] -= 1 # reset freqs return 1 res = dfs(cur.left) + dfs(cur.right) freqs[cur.val] -= 1 return res return dfs(root)
pseudo-palindromic-paths-in-a-binary-tree
Clean, Fast Python3 Explained | DFS & Counter | O(n)
ryangrayson
0
4
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,742
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573146/Python-or-Recursive-DFS-%2B-Counter-or-DFS-%2B-Bit-manipulation
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: def dfs(node: Optional[TreeNode], pathCounter: Counter) -> None: nonlocal pseudoPalindromeCount pathCounter[node.val] += 1 if node.left is None and node.right is None: if len([val for val in pathCounter.values() if val % 2 != 0]) <= 1: pseudoPalindromeCount += 1 else: for child in [node.left, node.right]: if child is not None: dfs(child, pathCounter) pathCounter[node.val] -= 1 pseudoPalindromeCount = 0 dfs(root, Counter()) return pseudoPalindromeCount
pseudo-palindromic-paths-in-a-binary-tree
Python | Recursive DFS + Counter | DFS + Bit-manipulation
sr_vrd
0
13
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,743
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/2573146/Python-or-Recursive-DFS-%2B-Counter-or-DFS-%2B-Bit-manipulation
class Solution: def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int: def dfs(node: Optional[TreeNode], pathCounter: int) -> None: nonlocal pseudoPalindromeCount pathCounter ^= 1 << (node.val) if node.left is None and node.right is None: if bin(pathCounter)[2:].count("1") <= 1: pseudoPalindromeCount += 1 else: for child in [node.left, node.right]: if child is not None: dfs(child, pathCounter) pseudoPalindromeCount = 0 dfs(root, 0) return pseudoPalindromeCount
pseudo-palindromic-paths-in-a-binary-tree
Python | Recursive DFS + Counter | DFS + Bit-manipulation
sr_vrd
0
13
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,744
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/1838889/Python-Solution-Using-Least-Significant-Bit-(LSB)-and-XOR
class Solution: def pseudoPalindromicPaths(self, root: Optional[TreeNode]) -> int: bits = 0 num_paths = 0 def is_leaf(root): if root and not root.left and not root.right: return True return False def dfs(root, level): nonlocal num_paths, bits if not root: return bits ^= 1 << root.val if is_leaf(root): if level % 2 == 0 and bits == 0: num_paths += 1 elif level % 2 == 1: lsb = bits &amp; -bits # least significant bit if bits - lsb == 0: num_paths += 1 dfs(root.left, level + 1) dfs(root.right, level + 1) bits ^= 1 << root.val # backtrack as (1 << root.val) ^ (1 << root.val) = 0 dfs(root, 1) return num_paths
pseudo-palindromic-paths-in-a-binary-tree
Python Solution Using Least Significant Bit (LSB) and XOR
user3088H
0
108
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,745
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/1113149/Python3-post-order-dfs
class Solution: def pseudoPalindromicPaths (self, root: TreeNode) -> int: def fn(node): """Post-order traverse the tree and update ans.""" nonlocal ans if not node: return freq[node.val-1] += 1 if not node.left and not node.right and sum(x&amp;1 for x in freq) <= 1: ans += 1 fn(node.left) fn(node.right) freq[node.val-1] -= 1 # backtracking ans, freq = 0, [0]*9 fn(root) return ans
pseudo-palindromic-paths-in-a-binary-tree
[Python3] post-order dfs
ye15
0
56
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,746
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/1113149/Python3-post-order-dfs
class Solution: def pseudoPalindromicPaths (self, root: TreeNode) -> int: def fn(node, mask): """Post-order traverse the tree and update ans.""" if not node: return 0 mask ^= 1 << node.val if not node.left and not node.right: # leaf node return 1 if mask &amp; (mask-1) == 0 else 0 return fn(node.left, mask) + fn(node.right, mask) return fn(root, 0)
pseudo-palindromic-paths-in-a-binary-tree
[Python3] post-order dfs
ye15
0
56
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,747
https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/discuss/653984/Python3-dfs-simple-solution-Pseudo-Palindromic-Paths-in-a-Binary-Tree
class Solution: def pseudoPalindromicPaths (self, root: TreeNode) -> int: def dfs(root: TreeNode, bitmap: int) -> int: if not root: return 0 bitmap ^= 1 << root.val if not root.left and not root.right: return int(bitmap &amp; (bitmap - 1) == 0) return dfs(root.left, bitmap) + dfs(root.right, bitmap) return dfs(root, 0)
pseudo-palindromic-paths-in-a-binary-tree
Python3 dfs simple solution - Pseudo-Palindromic Paths in a Binary Tree
r0bertz
0
130
pseudo palindromic paths in a binary tree
1,457
0.68
Medium
21,748
https://leetcode.com/problems/max-dot-product-of-two-subsequences/discuss/2613290/Python-Solution-or-DP-or-LCS
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: n=len(nums1) m=len(nums2) dp=[[0]*(m+1) for i in range(n+1)] for i in range(m+1): dp[0][i]=-1e9 for i in range(n+1): dp[i][0]=-1e9 for i in range(1, n+1): for j in range(1, m+1): val=nums1[i-1]*nums2[j-1]+max(0, dp[i-1][j-1]) dp[i][j]=max(val, max(dp[i-1][j], dp[i][j-1])) return dp[n][m]
max-dot-product-of-two-subsequences
Python Solution | DP | LCS
Siddharth_singh
0
8
max dot product of two subsequences
1,458
0.463
Hard
21,749
https://leetcode.com/problems/max-dot-product-of-two-subsequences/discuss/1113139/Python3-top-down-dp
class Solution: def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: @cache def fn(i, j): """Return max dot product of nums1[i:] and nums2[j:].""" if i == len(nums1) or j == len(nums2): return -inf return max(nums1[i]*nums2[j] + fn(i+1, j+1), nums1[i]*nums2[j], fn(i+1, j), fn(i, j+1)) return fn(0, 0)
max-dot-product-of-two-subsequences
[Python3] top-down dp
ye15
0
68
max dot product of two subsequences
1,458
0.463
Hard
21,750
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2730962/Easy-solution-using-dictionary-in-python
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: n, m = len(target), len(arr) if m > n: return False t = Counter(target) a = Counter(arr) for k, v in a.items(): if k in t and v == t[k]: continue else: return False return True
make-two-arrays-equal-by-reversing-subarrays
Easy solution using dictionary in python
ankurbhambri
7
89
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,751
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2151834/Python-Counter-1liner
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return Counter(arr) == Counter(target)
make-two-arrays-equal-by-reversing-subarrays
Python Counter 1liner
lokeshsenthilkumar
2
60
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,752
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2640958/Simple-Python-O(N)-Solution
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: if len(target)!=len(arr): return False for i in target: if i not in arr: return False target.sort() arr.sort() for i in range(len(arr)): if arr[i]!= target[i]: return False return True
make-two-arrays-equal-by-reversing-subarrays
Simple [Python O(N)] Solution
Sneh713
1
75
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,753
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1938250/easy-python-code
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: for i in target: if target.count(i) != arr.count(i) : return False return True
make-two-arrays-equal-by-reversing-subarrays
easy python code
dakash682
1
84
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,754
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1664475/Simplest-Solution-and-Well-Explained
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: dic1 = Counter(target) dic2 = Counter(arr) return dic1==dic2
make-two-arrays-equal-by-reversing-subarrays
📌📌 Simplest Solution and Well Explained 🐍
abhi9Rai
1
259
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,755
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1664475/Simplest-Solution-and-Well-Explained
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return sorted(target)==sorted(arr)
make-two-arrays-equal-by-reversing-subarrays
📌📌 Simplest Solution and Well Explained 🐍
abhi9Rai
1
259
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,756
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2825892/one-line
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: target.sort() arr.sort() if (target==arr): return True else: return False
make-two-arrays-equal-by-reversing-subarrays
one line
nishithakonuganti
0
1
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,757
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2818179/Python3-Solution-with-using-hashmap
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: c = collections.Counter(arr) for elem in target: if c[elem] == 0: return False c[elem] -= 1 return True
make-two-arrays-equal-by-reversing-subarrays
[Python3] Solution with using hashmap
maosipov11
0
2
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,758
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2542855/Easy-python-solution
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: target.sort() arr.sort() if arr != target: return False return True
make-two-arrays-equal-by-reversing-subarrays
Easy python solution
samanehghafouri
0
12
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,759
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2522233/Simple-python-solution-using-hashmap
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: if len(target) != len(arr): return False dic1 = Counter(target) dic2 = Counter(arr) for k,v in dic1.items(): if dic1[k] != dic2[k]: return False return True
make-two-arrays-equal-by-reversing-subarrays
Simple python solution using hashmap
aruj900
0
21
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,760
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2397885/Python3-Solution-Detailed-explanation-in-comments
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: from collections import Counter # Approach: # it can be observed that unless we have any element occurring more / less number # of times in arr than in target , we cant make them equal # else we can always make them equal return False if Counter(target)-Counter(arr) else True # if any element remains on subtracting from target then it means # it occurred more / less number of times than in target # so we cant make them equal # TC = O(n), SC = O(n) # As creating a counter takes O(n) time # similary it takes up O(n) space # n = number of elements in the (longer) list.
make-two-arrays-equal-by-reversing-subarrays
Python3 Solution Detailed explanation in comments
jinwooo
0
34
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,761
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2280771/2-Easy-Python-Solutions.........
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: lst=[] for i in arr: if i in target: lst.append(i) target.remove(i) else: return False return True
make-two-arrays-equal-by-reversing-subarrays
2 Easy Python Solutions.........
guneet100
0
54
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,762
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2191794/Python-1-liner
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return sorted(arr)==sorted(target)
make-two-arrays-equal-by-reversing-subarrays
Python 1 liner
hvt1998
0
57
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,763
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/2185433/Super-simple-python-one-liner
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return sorted(target) == sorted(arr)
make-two-arrays-equal-by-reversing-subarrays
Super simple python one-liner
pro6igy
0
41
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,764
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1991318/Python-Easiest-Solution-With-Explanation-or-98.89-Faster-or-Sorting-or-Beg-to-advor
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: target.sort() # sorting target list arr.sort() # sorting arr list if len(target) != len(arr): # if both list lenght doesnt match they have now scope of being equal. return False # false as they cant be. if target == arr: # after sorting we are checking if both list are equal or not return True # true if they are. return False # false if they arent.
make-two-arrays-equal-by-reversing-subarrays
Python Easiest Solution With Explanation | 98.89 % Faster | Sorting | Beg to adv|
rlakshay14
0
86
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,765
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1950391/make-two-arrays-equal
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: target.sort() arr.sort() if target==arr: return True else: return False
make-two-arrays-equal-by-reversing-subarrays
make two arrays equal
anil5829354
0
61
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,766
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1944726/Python-simple-5-line-solution-faster-than-99.5
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: if set(target) != set(arr): return False target.sort() arr.sort() return target == arr
make-two-arrays-equal-by-reversing-subarrays
Python simple 5-line solution, faster than 99.5%
akeates22
0
66
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,767
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1887304/Python-one-liner-solution
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return sorted(target) == sorted(arr)
make-two-arrays-equal-by-reversing-subarrays
Python one liner solution
alishak1999
0
79
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,768
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1804329/Python-3-Solution
class Solution: """Quick Sort Algorithm""" # Position of pivot element def pivot_place(self, array: List[int], first: int, last: int) -> int: pivot = array[first] left = first + 1 right = last while True: while left <= right and array[left] <= pivot: left += 1 while left <= right and array[right] >= pivot: right -= 1 if left > right: break else: array[left], array[right] = array[right], array[left] array[first], array[right] = array[right], array[first] return right # Divide and Conquer def quick_sort(self, array: List[int], first: int, last: int) -> None: if first <= last: p = self.pivot_place(array, first, last) self.quick_sort(array, first, p-1) self.quick_sort(array, p+1, last) def canBeEqual(self, target: List[int], arr: List[int]) -> bool: n = len(arr) m = len(target) self.quick_sort(arr, 0, n-1) self.quick_sort(target, 0, m-1) if target == arr: return True return False
make-two-arrays-equal-by-reversing-subarrays
Python 3 Solution
AprDev2011
0
73
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,769
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1638668/WEEB-DOES-PYTHON
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: arr.sort() target.sort() for i in range(len(target)): if target[i] != arr[i]: return False return True
make-two-arrays-equal-by-reversing-subarrays
WEEB DOES PYTHON
Skywalker5423
0
69
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,770
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1596497/Python-3-one-line-solution
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return collections.Counter(target) == collections.Counter(arr)
make-two-arrays-equal-by-reversing-subarrays
Python 3 one line solution
dereky4
0
157
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,771
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1158769/Easy-Python-Solution%3A-4-steps
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: #imp here is arr and target should have same values and we should be able to arrange arr values in one step if sorted(arr)==sorted(target): return(True) else: return(False)
make-two-arrays-equal-by-reversing-subarrays
Easy Python Solution: 4 steps
YashashriShiral
0
105
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,772
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1035615/Python3-easy-solution
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return sorted(target) == sorted(arr)
make-two-arrays-equal-by-reversing-subarrays
Python3 easy solution
EklavyaJoshi
0
79
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,773
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1010497/python3-solution
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: for i in arr: if i not in target: return False elif arr.count(i) != target.count(i): return False return True
make-two-arrays-equal-by-reversing-subarrays
python3 solution
izekchen0222
0
74
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,774
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/672868/PythonPython3-Two-ArraysEqual-one-liners
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return all(map(lambda x, y: x == y, sorted(target), sorted(arr)))
make-two-arrays-equal-by-reversing-subarrays
[Python/Python3] Two ArraysEqual one - liners
newborncoder
0
146
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,775
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/672868/PythonPython3-Two-ArraysEqual-one-liners
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return sorted(target) == sorted(arr)
make-two-arrays-equal-by-reversing-subarrays
[Python/Python3] Two ArraysEqual one - liners
newborncoder
0
146
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,776
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/672868/PythonPython3-Two-ArraysEqual-one-liners
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return Counter(target) == Counter(arr)
make-two-arrays-equal-by-reversing-subarrays
[Python/Python3] Two ArraysEqual one - liners
newborncoder
0
146
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,777
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/660582/Python3-one-line
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return sorted(target) == sorted(arr)
make-two-arrays-equal-by-reversing-subarrays
[Python3] one-line
ye15
0
39
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,778
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/660582/Python3-one-line
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return Counter(target) == Counter(arr)
make-two-arrays-equal-by-reversing-subarrays
[Python3] one-line
ye15
0
39
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,779
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1834328/1-Line-Python-Solution-oror-85-Faster-oror-Memory-less-than-65
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return sorted(target)==sorted(arr)
make-two-arrays-equal-by-reversing-subarrays
1-Line Python Solution || 85% Faster || Memory less than 65%
Taha-C
-1
110
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,780
https://leetcode.com/problems/make-two-arrays-equal-by-reversing-subarrays/discuss/1834328/1-Line-Python-Solution-oror-85-Faster-oror-Memory-less-than-65
class Solution: def canBeEqual(self, target: List[int], arr: List[int]) -> bool: return Counter(target)==Counter(arr)
make-two-arrays-equal-by-reversing-subarrays
1-Line Python Solution || 85% Faster || Memory less than 65%
Taha-C
-1
110
make two arrays equal by reversing subarrays
1,460
0.722
Easy
21,781
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2092441/Python-oror-2-Easy-oror-One-liner-with-explanation
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: return len({s[i:i+k] for i in range(len(s)-k+1)}) == 2 ** k
check-if-a-string-contains-all-binary-codes-of-size-k
✅ Python || 2 Easy || One liner with explanation
constantine786
26
1,900
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,782
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2092441/Python-oror-2-Easy-oror-One-liner-with-explanation
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: n=len(s) required_count=2 ** k seen=set() for i in range(n-k+1): tmp=s[i:i+k] if tmp not in seen: seen.add(tmp) required_count-=1 if required_count==0: # kill the loop once the condition is met return True return False
check-if-a-string-contains-all-binary-codes-of-size-k
✅ Python || 2 Easy || One liner with explanation
constantine786
26
1,900
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,783
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2092441/Python-oror-2-Easy-oror-One-liner-with-explanation
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: required_count=1<<k # same as 2 ^ k seen=[False] * required_count mask=required_count-1 hash_code=0 for i in range(len(s)): hash_code = ((hash_code<<1) &amp; mask) | (int(s[i])) if i>=k-1 and not seen[hash_code]: seen[hash_code]=True required_count-=1 if required_count==0: return True return False
check-if-a-string-contains-all-binary-codes-of-size-k
✅ Python || 2 Easy || One liner with explanation
constantine786
26
1,900
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,784
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2092441/Python-oror-2-Easy-oror-One-liner-with-explanation
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: required_count=1<<k seen=[0] * ((required_count-1)//32+1) mask=required_count-1 hash_code=0 def is_set(code): idx = code//32 bit_pos = code%32 return seen[idx] &amp; (1<<bit_pos) != 0 def set_bit(code): idx = code//32 bit_pos = code%32 seen[idx] |= (1<<bit_pos) for i in range(len(s)): hash_code = ((hash_code<<1) &amp; mask) | (int(s[i])) if i>=k-1 and not is_set(hash_code): set_bit(hash_code) required_count-=1 if required_count==0: return True return False
check-if-a-string-contains-all-binary-codes-of-size-k
✅ Python || 2 Easy || One liner with explanation
constantine786
26
1,900
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,785
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2092429/Python-one-liner
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: return len(set(s[i:i+k] for i in range(len(s)-k+1))) == 2**k
check-if-a-string-contains-all-binary-codes-of-size-k
Python, one-liner
blue_sky5
10
537
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,786
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2096528/python3-or-clean-code-or-short-solution-or-understandable-or-easy
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: s1=set() n=len(s)-k+1 for i in range(0,n): s1.add(s[i:i+k]) if len(s1)==2**k: return True else: return False
check-if-a-string-contains-all-binary-codes-of-size-k
python3 | clean code | short solution | understandable | easy
T1n1_B0x1
1
25
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,787
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2093552/java-python-bit-manipulations-and-sliding-window-(commented)
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: if len(s) - k + 1 < (1<<k) : return False #string not contain all numbers from the whole set table = [0]*(1<<k) #list for each number from the whole set num = 0 #for first number for sliding window with size = k mask = 1<<(k-1) #for discharging current bit (start - hi bit) for i in range(k) : #construct first number for sliding window if s[i] == '1' : num |= mask; #add current bit into number mask >>= 1 #shift bit in mask table[num] = 1 #and check this number in list as presented delete_hi_bit = (1<<(k-1)) - 1 #mask for deleting hi bit for i in range (k, len(s)) : #moving sliding window num &amp;= delete_hi_bit #deleting hi bit into number num <<= 1 #shifting number if s[i] == '1' : num |= 1 #adding low bit (if it present) table[num] = 1 #and check this number in list as presented for n in table : #checking allnumbers from the whole set if n == 0 : return False #if current number isn't presented return True #all nubers are presented
check-if-a-string-contains-all-binary-codes-of-size-k
java, python - bit manipulations & sliding window (commented)
ZX007java
1
68
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,788
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2093389/Solution-Using-Set-With-Explanation-in-Python.
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: length = len(s) #length of string s maxSize = 2**k #Max number of elements that can exist of size k #for 2**k elements to exist in a string its size should always be greater than (k-1)+2**k #example: if k=1 then length of s should be at least 2 for elements of length k=1 i.e., {0,1} to exist. if length < (k-1)+maxSize: return False #Using set here as it does not store duplicate values temp_set = set() #Now we loop from zero for i in range(length-k+1): temp_set.add(s[i:i+k]) #adding elements to set from s of length k #i.e, each time it adds elements s[0:2],then s[1:3] and so on if k=2 #and repeated values will not be stored as set don't store duplicates #in this way we generate all unique values of length k from s. #finally matching the length of set with number of elements of size k that can exist. return len(temp_set) == maxSize
check-if-a-string-contains-all-binary-codes-of-size-k
Solution Using Set With Explanation in Python.
AY_
1
12
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,789
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/1556998/Python-simple-O(n)-solution
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: d = set() c = 0 for r in range(len(s)-k+1): ss = s[r:r+k] if ss in d: continue d.add(ss) c += 1 return c == 2**k
check-if-a-string-contains-all-binary-codes-of-size-k
Python simple O(n) solution
cyrille-k
1
99
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,790
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/1106264/Sliding-Window-Hash-Python3-Solution
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: substrings = {} #dictionary p, q = 0, k #2 pointers amount_substrings = 0 #amount counter while q <= len(s): if s[p:q] not in substrings: substrings[s[p:q]] = p amount_substrings += 1 p += 1 q += 1 return amount_substrings == 2**k
check-if-a-string-contains-all-binary-codes-of-size-k
Sliding Window Hash Python3 Solution
ronald-luo
1
66
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,791
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2745387/Python-easy-to-understand-O(n.k)
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: #the max combination we can have is 2^k #Time: O(nk) #Space: O(nk) u_comb = set() for i in range(len(s)-k+1): u_comb.add(s[i:i+k]) return len(u_comb)==2**k
check-if-a-string-contains-all-binary-codes-of-size-k
Python easy to understand, O(n.k)
kartikchoudhary96
0
2
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,792
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2743022/check-if-a-string-contains-all-binary-codes-of-size-k
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: m=set() for i in range(len(s)-k+1): m.add(s[i:i+k]) return len(m)==2**k # l=[] # for i in range(2**k, 2**(k+1)): # l.append(bin(i)[3:]) # m=set() # for i in range(len(s)-k+1): # m.add(s[i:i+k]) # for i in l: # if i not in m: # return False # return True
check-if-a-string-contains-all-binary-codes-of-size-k
check-if-a-string-contains-all-binary-codes-of-size-k
shivansh2001sri
0
2
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,793
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2095960/Using-a-set
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: subs = set() for i in range(len(s) - k + 1): subs.add(s[i:i+k]) return len(subs) == 2**k
check-if-a-string-contains-all-binary-codes-of-size-k
Using a set
andrewnerdimo
0
5
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,794
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2095256/Python3-or-One-Liner-With-Explanation
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: return True if len({s[i:i+k] for i in range(len(s)-k+1)}) == (1<<k) else False
check-if-a-string-contains-all-binary-codes-of-size-k
Python3 | One Liner With Explanation
tg68
0
17
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,795
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2095222/Python-Solution
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: se=set() for i in range(0,len(s)): if i+k<=len(s): se.add(s[i:i+k]) return len(se)==2**k ```Python Solution
check-if-a-string-contains-all-binary-codes-of-size-k
Python Solution
a_dityamishra
0
10
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,796
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2095003/Optimal-Solution-using-Set
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: codes = set() for i in range(len(s)-k+1): codes.add(s[i:i+k]) if len(codes) == 2 ** k: return True return False
check-if-a-string-contains-all-binary-codes-of-size-k
Optimal Solution using Set
narasimharaomeda
0
7
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,797
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2094506/Python-easy-solution-for-beginners
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: codes = set() for i in range(len(s)): if s[i:i+k] not in codes and len(s[i:i+k]) == k: codes.add(s[i:i+k]) return len(codes) == 2 ** k
check-if-a-string-contains-all-binary-codes-of-size-k
Python easy solution for beginners
alishak1999
0
7
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,798
https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/discuss/2094474/Python-or-Just-3-lines-of-Code-with-explanation
class Solution: def hasAllCodes(self, s: str, k: int) -> bool: ''' Here insted of creating all the possible string of size k, we will create all unique string of size k using set and then, as we know if k = 2 then unique number of strings that can be formed is 2 ** k using this we will compare the size of set created and return ans accordingly. ''' # /////// Optimised Solution TC: O(n*k) ///////// hashset = set() for i in range(len(s) - k + 1): hashset.add(s[i:i+k]) return len(hashset) == 2 ** k
check-if-a-string-contains-all-binary-codes-of-size-k
Python | Just 3 lines of Code with explanation
__Asrar
0
14
check if a string contains all binary codes of size k
1,461
0.568
Medium
21,799