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/next-greater-element-i/discuss/2765312/Next-greater-element-python-O(nlogn)
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: nums2_sorted = sorted(range(len(nums2)), key = lambda x: nums2[x]) # [0, 3, 1, 2] nums1_sorted = sorted(range(len(nums1)), key = lambda x: nums1[x]) # [1, 2, 0] result = [-1]*len(nu...
next-greater-element-i
Next greater element - python O(nlogn)
DavidCastillo
0
4
next greater element i
496
0.714
Easy
8,700
https://leetcode.com/problems/next-greater-element-i/discuss/2764611/Python-Solution-beats-99.6-users-with-explanation-Fastest-solution
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: if not nums2: return None mapping = {} result = [] stack = [] stack.append(nums2[0]) for i in range(1, len(nums2)): while stack and nums2[i] > stack[-1]: # if stack is not...
next-greater-element-i
Python Solution beats 99.6% users with explanation Fastest solution
mritunjayyy
0
4
next greater element i
496
0.714
Easy
8,701
https://leetcode.com/problems/next-greater-element-i/discuss/2746312/easy-method-using-python
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: l=[] s=-1 for i in nums1: p=nums2.index(i) d=nums2[p:] i=d[0] u=0 for j in d: if(j>i): u=j ...
next-greater-element-i
easy method using python
sindhu_300
0
9
next greater element i
496
0.714
Easy
8,702
https://leetcode.com/problems/next-greater-element-i/discuss/2743106/python-Explanation
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: s=len(nums2) l1=[] l,stk=[],[] for i in range(s-1,-1,-1): if len(stk)==0: stk.append(nums2[i]) l.append(-1) elif len(stk)>0 and stk[...
next-greater-element-i
python Explanation
Kevin7777777
0
5
next greater element i
496
0.714
Easy
8,703
https://leetcode.com/problems/next-greater-element-i/discuss/2738322/Beats-97.66-with-Stacks-with-explanation
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: pairs = {} result = [] stack = [] stack.append(nums2[0]) def makePairs(num, greaterValue): pairs[num] = greaterValue for i in range(1, len(nums2)): ...
next-greater-element-i
Beats 97.66 % with Stacks with explanation
karanvirsagar98
0
8
next greater element i
496
0.714
Easy
8,704
https://leetcode.com/problems/next-greater-element-i/discuss/2734653/Python-34ms-Easy-Understanding
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: k=[] for i in nums1: for j in range(nums2.index(i),len(nums2)): if nums2[j]>i and len(k)<nums1.index(i)+1: k+=nums2[j], break ...
next-greater-element-i
Python 34ms Easy Understanding
Jlonerawesome
0
10
next greater element i
496
0.714
Easy
8,705
https://leetcode.com/problems/next-greater-element-i/discuss/2730520/Next-Greater-Element-I-or-Python-Solution
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: def findGreater(num): length = len(nums2) temp = nums2[nums2.index(num)+1:] for i in temp: if i > num: return i return -1 ...
next-greater-element-i
Next Greater Element I | Python Solution
ygygupta0
0
2
next greater element i
496
0.714
Easy
8,706
https://leetcode.com/problems/next-greater-element-i/discuss/2723590/Python-solution
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: hm={} for i in range(len(nums1)): for j in range(nums2.index(nums1[i]),len(nums2)): hm[i]=[] if nums2[j]>nums1[i]: hm[i].append(nums2[j]) ...
next-greater-element-i
Python solution
annazhengzhu
0
3
next greater element i
496
0.714
Easy
8,707
https://leetcode.com/problems/next-greater-element-i/discuss/2721317/Python-%3A-O(n2)-and-O(n)-solution-with-explaination
class Solution: #Order n^2 solution def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: nums1IndexMap = {v:i for i,v in enumerate(nums1)} #Searching array output = [-1]*len(nums1) for i,v in enumerate(nums2): if v not in nums1IndexMap: continue ...
next-greater-element-i
Python : O(n^2) and O(n) solution with explaination
abrarjahin
0
6
next greater element i
496
0.714
Easy
8,708
https://leetcode.com/problems/next-greater-element-i/discuss/2721317/Python-%3A-O(n2)-and-O(n)-solution-with-explaination
class Solution: #Order n solution using monotonic stack def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: nums1IndexMap = {v:i for i,v in enumerate(nums1)} #Searching array output = [-1]*len(nums1) searchingElementStack = [] #Should store only values if-...
next-greater-element-i
Python : O(n^2) and O(n) solution with explaination
abrarjahin
0
6
next greater element i
496
0.714
Easy
8,709
https://leetcode.com/problems/next-greater-element-i/discuss/2717244/Python3-64-faster-with-explanation
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: rlist = [] for num in nums1: i = nums2.index(num) if nums2[i:]: check = 0 for item in nums2[i:]: if item > nums2[i]: ...
next-greater-element-i
Python3, 64% faster with explanation
cvelazquez322
0
2
next greater element i
496
0.714
Easy
8,710
https://leetcode.com/problems/next-greater-element-i/discuss/2715303/Python-O(m%2Bn)-using-stack-with-easy-to-understand-approach
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: #Time-- O(n+m) #Space-- O(m) hset={n:i for i,n in enumerate(nums1)} stack =[] res=[-1]*len(nums1) for i in range(len(nums2)): while stack and nums2[...
next-greater-element-i
Python O(m+n) using stack with easy to understand approach
kartikchoudhary96
0
2
next greater element i
496
0.714
Easy
8,711
https://leetcode.com/problems/next-greater-element-i/discuss/2709432/O(nums1.length-%2B-nums2.length)-time-python3-solution
class Solution: # O(nums1.length + nums2.length) time, # O(nums2.length) space, # Approach: monotonic stack, hashmap def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: stack = [] next_greater = {} for index, num in enumerate(nums2): ...
next-greater-element-i
O(nums1.length + nums2.length) time python3 solution
destifo
0
3
next greater element i
496
0.714
Easy
8,712
https://leetcode.com/problems/next-greater-element-i/discuss/2697288/O(N)-solution-with-monotonic-stack
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: stack = [] hashmap = {} for n in nums2: while stack and n > stack[-1]: hashmap[stack.pop()] = n stack.append(n) while stack: hashmap[st...
next-greater-element-i
O(N) solution with monotonic stack
michaelniki
0
9
next greater element i
496
0.714
Easy
8,713
https://leetcode.com/problems/next-greater-element-i/discuss/2674219/Python-or-Stack-solution
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: arr = [-1 for i in range(10**4 + 1)] st = [nums2[0]] for i in range(1, len(nums2)): while st and nums2[i] > st[-1]: arr[st.pop()] = nums2[i] st.append(nums2[...
next-greater-element-i
Python | Stack solution
LordVader1
0
76
next greater element i
496
0.714
Easy
8,714
https://leetcode.com/problems/next-greater-element-i/discuss/2673430/Python-Solution
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: output=[] for i in range(len(nums1)): index=nums2.index(nums1[i]) lock=False for j in range(index,len(nums2)): if(nums2[j]>nums1[i] and lock==False): ...
next-greater-element-i
Python Solution
Phoenix_18
0
1
next greater element i
496
0.714
Easy
8,715
https://leetcode.com/problems/next-greater-element-i/discuss/2654012/Next-Greater-Element-using-stacks(python)
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: n=len(nums2) m=len(nums1) rge=[-1]*n stack=[] stack.append(nums2[-1]) for i in range(n-2,-1,-1): while stack and nums2[i]>=stack[-1]: stack.pop()...
next-greater-element-i
Next Greater Element using stacks(python)
kaushik555
0
5
next greater element i
496
0.714
Easy
8,716
https://leetcode.com/problems/next-greater-element-i/discuss/2643343/python-easy-solution-using-monotonic-stack-and-dictionary
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: n=len(nums1) m=len(nums2) ans=[0]*n d={} for i in range(n): d[nums1[i]]=i #print(d) stack=[] for i in range(m-1,-1,-1): while len(sta...
next-greater-element-i
python easy solution using monotonic stack and dictionary
tush18
0
70
next greater element i
496
0.714
Easy
8,717
https://leetcode.com/problems/next-greater-element-i/discuss/2605639/Python-Solution-or-2-solutions-or-Brute-Force-or-Stack-%2B-Hashmap-or-O(n%2Bm)
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: # Brute Force: Accepted, TC: O(len(nums1)*len(nums2)) # ans=[-1]*len(nums1) # for i in range(len(nums1)): # index=nums2.index(nums1[i]) # for j in range(index+1, le...
next-greater-element-i
Python Solution | 2 solutions | Brute Force | Stack + Hashmap | O(n+m)
Siddharth_singh
0
47
next greater element i
496
0.714
Easy
8,718
https://leetcode.com/problems/next-greater-element-i/discuss/2575428/Simple-dictonary-solution
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: dic = {} for i in nums1: if i in nums2: dic[i] = nums2.index(i) res = [] for k in dic.keys(): j = dic[k] while j < len(nums2): ...
next-greater-element-i
Simple dictonary solution
aruj900
0
39
next greater element i
496
0.714
Easy
8,719
https://leetcode.com/problems/next-greater-element-i/discuss/2572371/Python3-or-3-Different-Solutions-or-Optimal-Complexity
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: # Method 1: Brute Force T.C: O(nˆ3) S.C: O(1) ans = [-1] * len(nums1) for i in range(len(nums1)): for j in range(len(nums2)): if nums1[i] == nums2[j]: ...
next-greater-element-i
Python3 | 3 Different Solutions | Optimal Complexity
chawlashivansh
0
58
next greater element i
496
0.714
Easy
8,720
https://leetcode.com/problems/next-greater-element-i/discuss/2564311/python-solution-using-stack
class Solution: def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]: dict_greater = {} stack = [nums2[0]] for num in nums2[1:]: if num < stack[-1]: stack.append(num) while len(stack) > 0 and num > stack[-1]: ...
next-greater-element-i
python solution using stack
samanehghafouri
0
44
next greater element i
496
0.714
Easy
8,721
https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/1453111/Python-Binary-Search
class Solution: def __init__(self, rects: List[List[int]]): self.rects = rects self.search_space = [] for i, rect in enumerate(rects): a, b, c, d = rect self.search_space.append((d - b + 1) * (c - a + 1)) if i != 0: self.search_space[i] +=...
random-point-in-non-overlapping-rectangles
Python Binary Search
ypatel38
1
155
random point in non overlapping rectangles
497
0.393
Medium
8,722
https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/1923494/Python-easy-understanding-solution-with-comment
class Solution: def __init__(self, rects: List[List[int]]): self.rects = rects self.weights = [] # self.weights record the "points" one rectangle have respectively for i in range(len(rects)): num_points = (rects[i][2] - rects[i][0] + ...
random-point-in-non-overlapping-rectangles
Python easy - understanding solution with comment
byroncharly3
0
93
random point in non overlapping rectangles
497
0.393
Medium
8,723
https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/808568/Python3-sampling-by-weight
class Solution: def __init__(self, rects: List[List[int]]): self.rects = rects #store rectangle self.wt = [0] for x1, y1, x2, y2 in rects: wt = (x2 - x1 + 1) * (y2 - y1 + 1) # number of points self.wt.append(self.wt[-1] + wt) def pick(self) -> List[int]: ...
random-point-in-non-overlapping-rectangles
[Python3] sampling by weight
ye15
0
51
random point in non overlapping rectangles
497
0.393
Medium
8,724
https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/808568/Python3-sampling-by-weight
class Solution: def __init__(self, rects: List[List[int]]): self.rects = rects # store rectangles self.wt = [0] # weighted sampling for x1, y1, x2, y2 in rects: wt = (x2 - x1 + 1) * (y2 - y1 + 1) # wt ~ number of points self.wt.append(self.wt[-1] + wt) def p...
random-point-in-non-overlapping-rectangles
[Python3] sampling by weight
ye15
0
51
random point in non overlapping rectangles
497
0.393
Medium
8,725
https://leetcode.com/problems/diagonal-traverse/discuss/272114/Simple-Python-Solution-(with-comments)
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: res = [] if not matrix: return res # group values in matrix by the sum of their indices in a map map = {} for i in range(len(matrix) + len(matrix[0]) - 1): map[i] = [] ...
diagonal-traverse
Simple Python Solution (with comments)
AnthonyChao
5
612
diagonal traverse
498
0.581
Medium
8,726
https://leetcode.com/problems/diagonal-traverse/discuss/1508963/Python-solution-or-dictionary-or-sum-of-index
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: # Making a dictionary of keys = (row + col) as in a Diagonal treversal line (index of row + index of col) = constant. And values of Dictionary are elements of mat myDict = {} for i in range(len(mat)): ...
diagonal-traverse
[Python] solution | dictionary | sum of index
samirpaul1
3
97
diagonal traverse
498
0.581
Medium
8,727
https://leetcode.com/problems/diagonal-traverse/discuss/2406808/Python-solution
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: M,N = len(mat), len(mat[0]) diagonals = [] # traverse first column for i in range(len(mat)): idx_i = i idx_j = 0 diagonals.append([]) while idx_i >...
diagonal-traverse
Python solution
pivovar3al
2
84
diagonal traverse
498
0.581
Medium
8,728
https://leetcode.com/problems/diagonal-traverse/discuss/1362507/Intuitive-Python-solution-with-debugging-statements-to-run-through.
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: rows = len(mat)-1 # index upperbound columns = len(mat[0])-1 # index upperbound up = False # Next iteration flag ans = [mat[0][0]] lastPoint = [0,0] while True: r,c ...
diagonal-traverse
Intuitive Python solution with debugging statements to run through.
SathvikPN
2
74
diagonal traverse
498
0.581
Medium
8,729
https://leetcode.com/problems/diagonal-traverse/discuss/2226442/Python3-Simple-Solution
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: flip=0 res=[] R=len(mat) C=len(mat[0]) def checker(r,c): if r<0 or c<0 or r>=R or c>=C: return False return True def runner(r,c): temp=[]...
diagonal-traverse
Python3 Simple Solution
sonikartik2021
1
79
diagonal traverse
498
0.581
Medium
8,730
https://leetcode.com/problems/diagonal-traverse/discuss/2038679/Python-detailed-explanation-Tree-based-implementation
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: # special cases if len(mat) == 1: return mat[0] elif len(mat[0]) == 1: return [m[0] for m in mat] def neighb(x, y): # gives left and right child for dirx, diry in [(1,...
diagonal-traverse
Python detailed explanation [Tree based implementation]
96sayak
1
55
diagonal traverse
498
0.581
Medium
8,731
https://leetcode.com/problems/diagonal-traverse/discuss/1651119/Python%3AUsing-sum-of-indexes
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: m = len(mat) n = len(mat[0]) res = list() sum_dict = defaultdict(list) # Segregate elements as per the sum if indexes (i,j) for i in range(m): for j in range(n): ...
diagonal-traverse
Python:Using sum of indexes
jnaik
1
98
diagonal traverse
498
0.581
Medium
8,732
https://leetcode.com/problems/diagonal-traverse/discuss/861312/Python3-two-solutions
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix: return [] # edge case m, n = len(matrix), len(matrix[0]) # dimension ans = [] i = j = 0 stride = 1 for _ in range(m+n-1): val = [] ii,...
diagonal-traverse
[Python3] two solutions
ye15
1
82
diagonal traverse
498
0.581
Medium
8,733
https://leetcode.com/problems/diagonal-traverse/discuss/861312/Python3-two-solutions
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix: return [] # edge case ans = [] m, n = len(matrix), len(matrix[0]) # dimension i = j = 0 for _ in range(m*n): ans.append(matrix[i][j]) if (i+j)...
diagonal-traverse
[Python3] two solutions
ye15
1
82
diagonal traverse
498
0.581
Medium
8,734
https://leetcode.com/problems/diagonal-traverse/discuss/861312/Python3-two-solutions
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: ans = [] if matrix: m, n = len(matrix), len(matrix[0]) # dimensions i, j, di, dj = 0, 0, -1, 1 for _ in range(m*n): ans.append(matrix[i][j]) if 0 ...
diagonal-traverse
[Python3] two solutions
ye15
1
82
diagonal traverse
498
0.581
Medium
8,735
https://leetcode.com/problems/diagonal-traverse/discuss/2848652/python3-solution
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: d = defaultdict(list) for i in range(len(mat)): for j in range(len(mat[0])): d[i+j].append(mat[i][j]) arr = [] for i in d.items(): if i[0]%2 == 0: ...
diagonal-traverse
python3 solution
Cosmodude
0
1
diagonal traverse
498
0.581
Medium
8,736
https://leetcode.com/problems/diagonal-traverse/discuss/2816617/Python-Simple-and-Easy-Solution-With-Diagram
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: # get no of rows and cols row = len(mat) col = len(mat[0]) # get no of diagonals needs no_diagonals = [[] for _ in range(row+col -1)] # put every element of mat in coresp...
diagonal-traverse
Python Simple and Easy Solution With Diagram
devzohaib
0
7
diagonal traverse
498
0.581
Medium
8,737
https://leetcode.com/problems/diagonal-traverse/discuss/2796650/Beats-91.54-in-Memory
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: res=[] r=c=0 going_up=True rmax,cmax=len(mat),len(mat[0]) while len(res)< rmax*cmax: if going_up: while r>=0 and c<cmax: ...
diagonal-traverse
Beats 91.54% in Memory
avinash_konduri
0
3
diagonal traverse
498
0.581
Medium
8,738
https://leetcode.com/problems/diagonal-traverse/discuss/2785748/python-traversing-optimised-solution
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: # O(n), O(n) where n = len(rows) * len(cols) rows, cols = len(mat), len(mat[0]) res = [] cur_row = cur_col = 0 going_up = True while len(res) != rows * cols: if going_up: ...
diagonal-traverse
python traversing optimised solution
sahilkumar158
0
6
diagonal traverse
498
0.581
Medium
8,739
https://leetcode.com/problems/diagonal-traverse/discuss/2743450/Python-98.33-or-O-(M%2BN)
class Solution: def findDiagonalOrder(self, mat) : n=len(mat) m=len(mat[0]) ans=[] row,col=0,0 for d in range(m+n-1): temp=[] if d < n: row=d col=0 else: row= n-1 col= d-row ...
diagonal-traverse
Python 98.33 % | O (M+N)
user3904Q
0
4
diagonal traverse
498
0.581
Medium
8,740
https://leetcode.com/problems/diagonal-traverse/discuss/2683848/O(N)-Intuitive-Python-BFS-Solution
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: m,n = len(mat), len(mat[0]) lvls = [] added = set() q = [] def add(i,j): if (i,j) not in added: added.add((i,j)) q.append((i,j)) add(0,0) ...
diagonal-traverse
O(N) Intuitive Python BFS Solution
Dylan_Yifan
0
10
diagonal traverse
498
0.581
Medium
8,741
https://leetcode.com/problems/diagonal-traverse/discuss/2678714/Python3-or-Simple-or-Intuitive-or-Neat
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: m, n, r, c = len(mat), len(mat[0]), 0, 0 diag = [] def getNext(r, c): # go Northeast if (r+c)%2 == 0 and r > 0 and c < n-1: return r-1, c+1 ...
diagonal-traverse
Python3 | Simple | Intuitive | Neat
aashi111989
0
22
diagonal traverse
498
0.581
Medium
8,742
https://leetcode.com/problems/diagonal-traverse/discuss/2558455/Python3-or-Using-Stack-or-O(m*n)
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: stack_1,stack_2=[],[] stack_1.append((0,0)) ans=[mat[0][0]] r,c=len(mat),len(mat[0]) mat[0][0]=float('inf') while stack_1 or stack_2: while stack_1: x,y=stack_1...
diagonal-traverse
[Python3] | Using Stack | O(m*n)
swapnilsingh421
0
23
diagonal traverse
498
0.581
Medium
8,743
https://leetcode.com/problems/diagonal-traverse/discuss/2455706/Python-3-or-Iterative-or-Direction-Check
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: m, n = len(mat), len(mat[0]) self.res = [] q = deque() q.append((0,0,True)) while q: i, j, goesUp = q.popleft() if i < 0 or i >= m or j < 0 or j >= n:...
diagonal-traverse
Python 3 | Iterative | Direction Check
Ploypaphat
0
46
diagonal traverse
498
0.581
Medium
8,744
https://leetcode.com/problems/diagonal-traverse/discuss/2408554/Python-Implementation-Using-Hashmap
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: ele = {} for i in range(len(mat)): for j in range(len(mat[i])): if i+j not in ele: ele[i+j] = [mat[i][j]] else: ele[i+j].append(mat[i][j...
diagonal-traverse
Python Implementation Using Hashmap
Abhi_-_-
0
37
diagonal traverse
498
0.581
Medium
8,745
https://leetcode.com/problems/diagonal-traverse/discuss/2334066/Easy-Simple-Python-Defaultdict(List)
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: # Default dict for simpler code d = defaultdict(list) for i in range(len(mat)): for j in range(len(mat[0])): # Add values to diagonal dictionary d[i+j].append(mat[i][j]) a...
diagonal-traverse
Easy Simple Python Defaultdict(List)
drblessing
0
23
diagonal traverse
498
0.581
Medium
8,746
https://leetcode.com/problems/diagonal-traverse/discuss/2074320/Simple-solution-Python-O(NM)-time-and-O(1)-space
class Solution: def change_direction(self): self.isign *= -1 self.jsign *= -1 def i_overflow(self, i): return i+self.isign < 0 or i+self.isign > self.m-1 def j_overflow(self, j): return j+self.jsign < 0 or j+self.jsign > self.n-1 def next_cell(self, i, j): ...
diagonal-traverse
Simple solution, Python, O(NM) time and O(1) space
tushar-rishav
0
43
diagonal traverse
498
0.581
Medium
8,747
https://leetcode.com/problems/diagonal-traverse/discuss/1872793/Simplest-Python-Solution-%2B-Visual-Explanation-Time%3A-O(M*N)-Space%3A-O(1)-or-grandma-can-understand
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: m,n=len(mat),len(mat[0]) res=[] for k in range(m+n-1): if k%2==0: i=min(m-1,k) j=k-i while 0<=i<m and 0<=j<n: res.append(mat[i][j])...
diagonal-traverse
Simplest Python Solution + Visual Explanation, Time: O(M*N), Space: O(1) | 🥴 grandma can understand
noobcoderbaby
0
45
diagonal traverse
498
0.581
Medium
8,748
https://leetcode.com/problems/diagonal-traverse/discuss/1865220/Python-iterative-dfs
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: rows, cols = len(mat), len(mat[0]) stack = [(0,0,0)] # (row, column, direction), 0 is up direction and 1 is down direction output = [] n = rows * cols while len(output) != n: ...
diagonal-traverse
Python iterative dfs
Rush_P
0
54
diagonal traverse
498
0.581
Medium
8,749
https://leetcode.com/problems/diagonal-traverse/discuss/1789445/Easy-money
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: m = len(mat) n = len(mat[0]) res = [] for s in range(m+n-1): if s%2 == 0: # even, go from left to right for j in range(max(s-m+1,0), min(s+1,n)): res.append...
diagonal-traverse
Easy money
justicesuker
0
38
diagonal traverse
498
0.581
Medium
8,750
https://leetcode.com/problems/diagonal-traverse/discuss/1750946/quick-easy-to-understand-python
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: row_dict =defaultdict(list) for row in range(len(mat)): for col in range(len(mat[0])): row_dict[row+col].append(mat[row][col]) flag = False res = [] for i,val in ...
diagonal-traverse
quick, easy to understand, python
aaronat
0
124
diagonal traverse
498
0.581
Medium
8,751
https://leetcode.com/problems/diagonal-traverse/discuss/1684420/Python-no-reverse-diagonal-iteration-simulation
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: n, m = len(mat), len(mat[0]) res = [] for s in range(0, n + m - 1): if s % 2 == 0: x = min(s, n - 1) y = s - x while x >= 0 and y < m: ...
diagonal-traverse
Python no-reverse diagonal iteration simulation
Krymore
0
59
diagonal traverse
498
0.581
Medium
8,752
https://leetcode.com/problems/diagonal-traverse/discuss/1600012/Easy-Python-very-simple-cases
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: tr = True row = col = 0 num_row = len(mat) num_col = len(mat[0]) ordered = [] while row < num_row and col < num_col: ordered.append(mat[row][col]) ...
diagonal-traverse
Easy Python - very simple cases
AbitamimBharmal
0
84
diagonal traverse
498
0.581
Medium
8,753
https://leetcode.com/problems/diagonal-traverse/discuss/1500518/Python3
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: d = collections.defaultdict(list) for i in range(len(mat)): for j in range(len(mat[0])): d[i+j].append(mat[i][j]) res = [] index = 0 for k, v ...
diagonal-traverse
Python3
immutable_23
0
73
diagonal traverse
498
0.581
Medium
8,754
https://leetcode.com/problems/diagonal-traverse/discuss/1500449/Easy-Python-Solution-with-greater-Direction-Vector-greater
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: index = (1, -1) direction = (-1, 1) arr = [] while len(arr) != len(mat) * len(mat[0]): index = (index[0] + direction[0], index[1] + direction[1]) if (index[0] < 0 or index...
diagonal-traverse
Easy Python Solution with -> Direction Vector ->
schedutron
0
56
diagonal traverse
498
0.581
Medium
8,755
https://leetcode.com/problems/diagonal-traverse/discuss/1500448/Easy-Python-Solution-with-greater-Direction-Vector-greater
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: index = (1, -1) direction = (-1, 1) arr = [] while len(arr) != len(mat) * len(mat[0]): index = (index[0] + direction[0], index[1] + direction[1]) if (index[0] < 0 or index...
diagonal-traverse
Easy Python Solution with -> Direction Vector ->
schedutron
0
27
diagonal traverse
498
0.581
Medium
8,756
https://leetcode.com/problems/diagonal-traverse/discuss/1431210/Simple-Python
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: num_bins = len(mat) + len(mat[0]) - 1 bins = [[] for _ in range(num_bins)] index = 0 for i in range(len(mat)): index = i for j in range(len(mat[0])): bins[index]...
diagonal-traverse
Simple Python
envy7
0
83
diagonal traverse
498
0.581
Medium
8,757
https://leetcode.com/problems/diagonal-traverse/discuss/1386313/Python-Solution
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: diagonal_order = [] n = len(matrix) m = len(matrix[0]) i = 0 j = 0 while len(diagonal_order) < n * m: while i >= 0 and j < m: # we go upwards diagonal_order...
diagonal-traverse
Python Solution
mariandanaila01
0
133
diagonal traverse
498
0.581
Medium
8,758
https://leetcode.com/problems/diagonal-traverse/discuss/1099305/Python-or-RuntimeSpace-Efficiency-99-or-Simple
class Solution: def findDiagonalOrder(self, mat: List[List[int]]) -> List[int]: if mat==[]: return mat r,c=len(mat),len(mat[0]) if r==1 or c==1: return [mat[i][j] for i in range(r) for j in range(c)] def getDiag(i,j,rev): diag=[] while j<c...
diagonal-traverse
Python | Runtime/Space Efficiency 99% | Simple
rajatrai1206
0
129
diagonal traverse
498
0.581
Medium
8,759
https://leetcode.com/problems/diagonal-traverse/discuss/1033801/Straight-forward-Python-solution
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix: return [] M = len(matrix) N = len(matrix[0]) result = {} for i in range(M): for j in range(N): if i+j not in result: ...
diagonal-traverse
Straight forward Python solution
sirajali05
0
123
diagonal traverse
498
0.581
Medium
8,760
https://leetcode.com/problems/diagonal-traverse/discuss/1020147/Python-Clean-solution-based-on-coordinates-w-comments-(TC%3A-O(nm)-SC%3A-O(1))
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: """ Time Complexity: O(nm), n - number of rows, m - number of cols Space Complexity: O(1), Note that the space occupied by the output array doesn't count towards the space complexity since that is a requiremen...
diagonal-traverse
Python Clean solution based on coordinates /w comments (TC: O(nm), SC: O(1))
elzzz
0
63
diagonal traverse
498
0.581
Medium
8,761
https://leetcode.com/problems/diagonal-traverse/discuss/914468/Python3-Easy-solution
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: d = defaultdict(list) for i in range(len(matrix)): for j in range(len(matrix[0])): d[i+j].append(matrix[i][j]) res = [] ...
diagonal-traverse
Python3 Easy solution
ermolushka2
0
127
diagonal traverse
498
0.581
Medium
8,762
https://leetcode.com/problems/diagonal-traverse/discuss/487810/Python3-super-simple-solution
class Solution: def findDiagonalOrder(self, matrix: List[List[int]]) -> List[int]: if not matrix: return [] temp = [] for i in range(len(matrix)): for j in range(len(matrix[0])): if len(temp)-1<i+j: temp.append([matrix[i][j]]) else: temp[i+j].appen...
diagonal-traverse
Python3 super simple solution
jb07
0
115
diagonal traverse
498
0.581
Medium
8,763
https://leetcode.com/problems/keyboard-row/discuss/1525751/Easy-Python-Solution-or-Faster-than-97-(24-ms)
class Solution: def findWords(self, wds: List[str]) -> List[str]: st = {'q': 1, 'w': 1, 'e': 1, 'r': 1, 't': 1, 'y': 1, 'u': 1, 'i': 1, 'o': 1, 'p': 1, 'a': 2, 's': 2, 'd': 2, 'f': 2, 'g': 2, 'h': 2, 'j': 2, 'k': 2, 'l': 2, 'z': 3, 'x': 3, 'c': 3, 'v': 3, 'b': 3, 'n': 3, 'm': 3} ret = [] f...
keyboard-row
Easy Python Solution | Faster than 97% (24 ms)
the_sky_high
5
462
keyboard row
500
0.692
Easy
8,764
https://leetcode.com/problems/keyboard-row/discuss/437422/C%2B%2BJavaPython-RegEx
class Solution: def findWords(self, words): letters_a, letters_b, letters_c = set('qwertyuiop'), set('asdfghjkl'), set('zxcvbnm') row_words = [] for word in words: unique_word = set(word.lower()) if unique_word <= letters_a or unique_word <= letters_b or unique_word <...
keyboard-row
[C++/Java/Python] RegEx
i-i
5
366
keyboard row
500
0.692
Easy
8,765
https://leetcode.com/problems/keyboard-row/discuss/437422/C%2B%2BJavaPython-RegEx
class Solution: def findWords(self, words): keyboard = r'(?i)^(?:[qwertyuiop]+|[asdfghjkl]+|[zxcvbnm]+)$' row_words = [] for word in words: if word == "": row_words.append("") row_words.extend(re.findall(keyboard, word)) return row_words
keyboard-row
[C++/Java/Python] RegEx
i-i
5
366
keyboard row
500
0.692
Easy
8,766
https://leetcode.com/problems/keyboard-row/discuss/1058707/Python3-simple-solution-using-%22set%22
class Solution: def findWords(self, words: List[str]) -> List[str]: l = ["qwertyuiop", "asdfghjkl", "zxcvbnm"] ans = [] for i in words: a = i.lower() if len(set(a).difference(set(l[0]))) == 0 or len(set(a).difference(set(l[1]))) == 0 or len(set(a).difference(set(l[2])...
keyboard-row
Python3 simple solution using "set"
EklavyaJoshi
4
126
keyboard row
500
0.692
Easy
8,767
https://leetcode.com/problems/keyboard-row/discuss/230863/Python-simple-solution
class Solution(object): def findWords(self, words): """ :type words: List[str] :rtype: List[str] """ word_list=[] top_row=set('qwertyuiop') mid_row=set('asdfghjkl') bottom_row=set('zxcvbnm') for word in words: if set(word.lower()).i...
keyboard-row
Python simple solution
kishoreravi97
4
748
keyboard row
500
0.692
Easy
8,768
https://leetcode.com/problems/keyboard-row/discuss/1474014/python-or-simple-or-Faster-than-96.17
class Solution: def findWords(self, words: List[str]) -> List[str]: set1 = set('qwertyuiop') set2 = set('asdfghjkl') set3 = set('zxcvbnm') res = list() for word in words: w = set(word.lower()) if len(w | set1) == len(set1) or len(w | set2) == len(set2)...
keyboard-row
python | simple | Faster than 96.17 %
deep765
3
239
keyboard row
500
0.692
Easy
8,769
https://leetcode.com/problems/keyboard-row/discuss/1353116/Python-Solution-using-Set-28ms
class Solution: def findWords(self, words: List[str]) -> List[str]: row1 = set('qwertyuiopQWERTYUIOP') row2 = set('asdfghjklASDFGHJKL') row3 = set('zxcvbnmZXCVBNM') result = [] for word in words: w = set(list(word)) if w.issubset(row1) or w.issubset(ro...
keyboard-row
Python Solution using Set - 28ms
_Mansiii_
3
174
keyboard row
500
0.692
Easy
8,770
https://leetcode.com/problems/keyboard-row/discuss/1879448/Python-solution-faster-than-95
class Solution: def findWords(self, words: List[str]) -> List[str]: row1 = "qwertyuiop" row2 = "asdfghjkl" row3 = "zxcvbnm" res = [] for i in words: if i[0].lower() in row1: if all(x in row1 for x in i.lower()): res.append(i) ...
keyboard-row
Python solution faster than 95%
alishak1999
2
181
keyboard row
500
0.692
Easy
8,771
https://leetcode.com/problems/keyboard-row/discuss/2594367/Python-simple-solution-(using-set-to-find-common-letters)
class Solution: def findWords(self, words: list[str]) -> list[str]: def is_valid(s): s, r1, r2, r3 = map(set, [s, 'qwertyuiop', 'asdfghjkl', 'zxcvbnm']) return s <= r1 or s <= r2 or s <= r3 return [i for i in words if is_valid(i.lower())]
keyboard-row
Python simple solution (using set to find common letters)
Mark_computer
1
27
keyboard row
500
0.692
Easy
8,772
https://leetcode.com/problems/keyboard-row/discuss/2408563/Python-faster-than-99.78-using-dict
class Solution: def findWords(self, words: List[str]) -> List[str]: d = {} for c in "qwertyuiop": d[c] = 1 for c in "asdfghjkl": d[c] = 2 for c in "zxcvbnm": d[c] = 3 result = [] for word in words: if len(wo...
keyboard-row
Python faster than 99.78 using dict
Mohan01234
1
211
keyboard row
500
0.692
Easy
8,773
https://leetcode.com/problems/keyboard-row/discuss/1993013/My-Solution-Without-python3-built-ins
class Solution: def findWords(self, words: List[str]) -> List[str]: charMap = {'q': 1, 'w': 1, 'e': 1, 'r': 1, 't': 1, 'y': 1, 'u': 1, 'i': 1, 'o': 1, 'p': 1, 'a': 2, 's': 2, 'd': 2, 'f': 2, 'g': 2, 'h': 2, 'j': 2, 'k': 2, 'l': 2, 'z': 3, 'x': 3, 'c': 3, 'v': 3, 'b': 3, 'n': 3, 'm': 3} result = [] ...
keyboard-row
My Solution Without python3 built-ins
huyanguyen3695
1
67
keyboard row
500
0.692
Easy
8,774
https://leetcode.com/problems/keyboard-row/discuss/1746680/Python-solution-(Using-Subset)
class Solution: def findWords(self, words: List[str]) -> List[str]: first_row_keywords = set("qwertyuiop") second_row_keywords = set("asdfghjkl") third_row_keywords = set("zxcvbnm") result_list = [] for word in words: word_to_set = set(word.lower()) # Checking ...
keyboard-row
Python solution (Using Subset)
aakshay740
1
83
keyboard row
500
0.692
Easy
8,775
https://leetcode.com/problems/keyboard-row/discuss/1695414/**-Python-code%3A
class Solution: def findWords(self, words: List[str]) -> List[str]: row1 = set("QWERTYUIOPqwertyuiop") row2 = set("ASDFGHJKLasdfghjkl") row3 = set("ZXCVBNMzxcvbnm") res=[] for word in words: check=True if word[0] in row1:...
keyboard-row
** Python code:
Anilchouhan181
1
68
keyboard row
500
0.692
Easy
8,776
https://leetcode.com/problems/keyboard-row/discuss/1468568/Easy-to-understand-Set-Operations-PythonPython3-Python3
class Solution: def findWords(self, words: List[str]) -> List[str]: row1 = {'q','w','e','r','t','y','u','i','o','p'} row2 = {'a','s','d','f','g','h','j','k','l'} row3 = {'z','x','c','v','b','n','m'} common = [] for word in words: string = set(word.lower()) ...
keyboard-row
Easy to understand, Set Operations, Python/Python3, Python3
AshwinBalaji52
1
150
keyboard row
500
0.692
Easy
8,777
https://leetcode.com/problems/keyboard-row/discuss/1404858/Python-Easily-Understandable-or-Runtime%3A-28-ms
class Solution: def findWords(self, words: List[str]) -> List[str]: fr="qwertyuiop" sr="asdfghjkl" tr="zxcvbnm" ans=[] for j in words: if all(i.lower() in fr for i in j) or all(i.lower() in sr for i in j) or all(i.lower() in tr for i in j): ans.app...
keyboard-row
Python Easily Understandable | Runtime: 28 ms
satyamshrma
1
103
keyboard row
500
0.692
Easy
8,778
https://leetcode.com/problems/keyboard-row/discuss/1292284/Python3-dollarolution(99-Faster)
class Solution: def findWords(self, words: List[str]) -> List[str]: r1 = 'qwertyuiop' r2 = 'asdfghjkl' r3 = 'zxcvbnm' m = [] for i in range(len(words)): f = 0 k = words[i].lower() for j in k: if j in r1: ...
keyboard-row
Python3 $olution(99% Faster)
AakRay
1
296
keyboard row
500
0.692
Easy
8,779
https://leetcode.com/problems/keyboard-row/discuss/1217990/python-solution
class Solution: def check1(self,s): l = "qwertyuiopQWERTYUIOP" for c in s: if not c in l: return False return True def check2(self,s): l = "asdfghjklASDFGHJKL" for c in s: if not c in l: return False ret...
keyboard-row
python solution
ArnabCk
1
75
keyboard row
500
0.692
Easy
8,780
https://leetcode.com/problems/keyboard-row/discuss/577650/Python-using-bit-mask
class Solution: def bitset(self, word): r = 0 for c in word: r |= 1 << (ord(c)-97) return r def findWords(self, words: List[str]) -> List[str]: r1 = self.bitset('qwertyuiop') r2 = self.bitset('asdfghjkl') r3 = self.bitset('zxcvbnm') res = [] ...
keyboard-row
Python, using bit mask
karbayev
1
103
keyboard row
500
0.692
Easy
8,781
https://leetcode.com/problems/keyboard-row/discuss/2836433/Python-Simple-Solution
class Solution: def findWords(self, words): inSet = [] topSet = set(['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P']) middleSet = set(['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', ...
keyboard-row
Python - Simple Solution
BladeStew
0
2
keyboard row
500
0.692
Easy
8,782
https://leetcode.com/problems/keyboard-row/discuss/2821282/Simple-solution-using-brute-force
class Solution: def findWords(self, words: List[str]) -> List[str]: return [z for z in words if sum(all(k.lower() in l for k in z)for l in['qwertyuiop','asdfghjkl','zxcvbnm']) > 0]
keyboard-row
Simple solution using brute force
alex41542
0
3
keyboard row
500
0.692
Easy
8,783
https://leetcode.com/problems/keyboard-row/discuss/2820595/Simple-for-loop
class Solution: def findWords(self, words: List[str]) -> List[str]: keyboard = ["qwertyuiop","asdfghjkl","zxcvbnm"] result = [] for word in words: if word[0].lower() in keyboard[0]: idx = 0 elif word[0].lower() in keyboard[1]: idx = 1 ...
keyboard-row
Simple for loop
aruj900
0
5
keyboard row
500
0.692
Easy
8,784
https://leetcode.com/problems/keyboard-row/discuss/2761671/Simple-Python
class Solution: def findWords(self, words: List[str]) -> List[str]: row1 = set("qwertyuiop") row2 = set("asdfghjkl") row3 = set("zxcvbnm") ans = [] for word in words: w = word.lower() if (all(ch in row1 for ch in w) or all(ch in row2...
keyboard-row
Simple Python
on_danse_encore_on_rit_encore
0
4
keyboard row
500
0.692
Easy
8,785
https://leetcode.com/problems/keyboard-row/discuss/2751887/One-liner-python-using-sets-Easy-solution
class Solution: def findWords(self, words: List[str]) -> List[str]: f = set('qwertyuiop') s = set('asdfghjkl') t = set('zxcvbnm') return [word for word in words if len(set(word.lower()) - f) == 0 or len(set(word.lower()) - s) == 0 or len(set(word.lower()) - t) == 0]
keyboard-row
One liner python using sets - Easy solution
jacobsimonareickal
0
3
keyboard row
500
0.692
Easy
8,786
https://leetcode.com/problems/keyboard-row/discuss/2744515/Solution-Using-Python
class Solution: def findWords(self, words: List[str]) -> List[str]: set1 = {'q','w','e','r','t','y','u','i','o','p'} set2 = {'a','s','d','f','g','h','j','k','l'} set3 = {'z','x','c','v','b','n','m'} res = [] for i in words: wordset = set(i.lower()) ...
keyboard-row
Solution Using Python
dnvavinash
0
5
keyboard row
500
0.692
Easy
8,787
https://leetcode.com/problems/keyboard-row/discuss/2520882/Python-oror-easy-and-well-explained-solution
class Solution(object): def findWords(self, words): """ :type words: List[str] :rtype: List[str] """ firstRow = "qwertyuiop" secondRow = "asdfghjkl" thirdRow = "zxcvbnm" result = [] for i in range(len(words)): add = words[i...
keyboard-row
Python || easy and well explained solution
ride-coder
0
27
keyboard row
500
0.692
Easy
8,788
https://leetcode.com/problems/keyboard-row/discuss/2175152/Python-Faster-than-96
class Solution: def findWords(self, words: List[str]) -> List[str]: first_row = set("q w e r t y u i o p Q W E R T Y U I O P".split()) second_row = set("a s d f g h j k l A S D F G H J K L".split()) third_row = set("z x c v b n m Z X C V B N M".split()) results = [] ...
keyboard-row
[Python] Faster than 96%
julenn
0
112
keyboard row
500
0.692
Easy
8,789
https://leetcode.com/problems/keyboard-row/discuss/2127129/Python-solution
class Solution: def findWords(self, words: List[str]) -> List[str]: row1 = list('qwertyuiop') row2 = list('asdfghjkl') row3 = list('zxcvbnm') final_list = [] for word in words: char_list = list(word.lower()) if all(i in row1 for i in char_list) or all(i in row2 for i in char_list) or...
keyboard-row
Python solution
NiketaM
0
98
keyboard row
500
0.692
Easy
8,790
https://leetcode.com/problems/keyboard-row/discuss/2096300/Python3-brute-force
class Solution: def findWords(self, words: List[str]) -> List[str]: rows, output = ["qwertyuiop", "asdfghjkl", "zxcvbnm"], [] for word in words: i, rowI, wordInRow, wLen = 0, 0, 0, len(word) while True: if i == wLen: ...
keyboard-row
[Python3] brute force
Shiyinq
0
41
keyboard row
500
0.692
Easy
8,791
https://leetcode.com/problems/keyboard-row/discuss/2025722/Python-set-solution
class Solution: def findWords(self, words: List[str]) -> List[str]: f = set("qwertyuiop") s = set("asdfghjkl") t = set("zxcvbnm") ans = [] for word in words: if len(set(word.lower()) | f) == len(f) or len(set(word.lower()) | s) == len(s) or len(set(word.lower()) |...
keyboard-row
Python set solution
StikS32
0
105
keyboard row
500
0.692
Easy
8,792
https://leetcode.com/problems/keyboard-row/discuss/2010751/Easy-and-short-python-solution
class Solution: def findWords(self, words: List[str]) -> List[str]: keyRows = ['qwertyusiop', 'asdfghjkl', 'zxcvbnm'] printable = [] for word in words: for keyRow in keyRows: if set(word.lower()).issubset(set(keyRow)): printable.append(word) ...
keyboard-row
Easy and short python solution
ssshekhu53
0
64
keyboard row
500
0.692
Easy
8,793
https://leetcode.com/problems/keyboard-row/discuss/1980907/Python-one-line-faster-than-88.11-(29)-Memory-usage-less-than-97.81-(13.8MB)
class Solution: def findWords(self, words: list[str]) -> list[str]: return [i for i in words if all(1 if x in tuple("qwertyuiop") else 0 for x in i.lower()) or all(1 if x in tuple("asdfghjkl") else 0 for x in i.lower()) or all(1 if x in tuple("zxcvbnm") else 0 for x in i.lower())]
keyboard-row
Python one-line faster than 88.11% (29), Memory usage less than 97.81% (13.8MB)
Minh4893IT
0
63
keyboard row
500
0.692
Easy
8,794
https://leetcode.com/problems/keyboard-row/discuss/1940151/Python-Clean-and-Simple!-Solution-%2B-One-Liner
class Solution: def findWords(self, words): def helper(w, r): return all(c in r for c in set(w.lower())) r1, r2, r3 = "qwertyuiop", "asdfghjkl", "zxcvbnm" return [w for w in words if helper(w,r1) or helper(w,r2) or helper(w,r3)]
keyboard-row
Python - Clean and Simple! Solution + One-Liner
domthedeveloper
0
76
keyboard row
500
0.692
Easy
8,795
https://leetcode.com/problems/keyboard-row/discuss/1940151/Python-Clean-and-Simple!-Solution-%2B-One-Liner
class Solution: def findWords(self, words): def helper(w, r): return all(c in r for c in set(w.lower())) return [w for w in words if helper(w,"qwertyuiop") or helper(w,"asdfghjkl") or helper(w,"zxcvbnm")]
keyboard-row
Python - Clean and Simple! Solution + One-Liner
domthedeveloper
0
76
keyboard row
500
0.692
Easy
8,796
https://leetcode.com/problems/keyboard-row/discuss/1940151/Python-Clean-and-Simple!-Solution-%2B-One-Liner
class Solution: def findWords(self, words): return (lambda f : [w for w in words if f(w,"qwertyuiop") or f(w,"asdfghjkl") or f(w,"zxcvbnm")])(lambda w,r : all(c in r for c in set(w.lower())))
keyboard-row
Python - Clean and Simple! Solution + One-Liner
domthedeveloper
0
76
keyboard row
500
0.692
Easy
8,797
https://leetcode.com/problems/keyboard-row/discuss/1826664/python-solution-Siimple
class Solution: def findWords(self, words: List[str]) -> List[str]: first_row = "qwertyuiop" second_row = "asdfghjkl" third_row = "zxcvbnm" output = [] # iterate through for word in words: # use score to keep track of the different rows score = [0,...
keyboard-row
python solution- Siimple
Tobi_Akin
0
47
keyboard row
500
0.692
Easy
8,798
https://leetcode.com/problems/keyboard-row/discuss/1800960/6-Lines-Python-Solution-oror-60-Faster-(38-ms)-oror-Memory-less-than-90
class Solution: def findWords(self, words: List[str]) -> List[str]: rows, ans = ["qwertyuiop", "asdfghjkl","zxcvbnm"], [] for word in words: row = [row for row in rows if word[0].lower() in row][0] for char in list(word): if char.lower() not in row: ans.append...
keyboard-row
6-Lines Python Solution || 60% Faster (38 ms) || Memory less than 90%
Taha-C
0
68
keyboard row
500
0.692
Easy
8,799