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/array-nesting/discuss/1453172/Python-super-simple-O(n)-O(1) | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
n = len(nums)
visited = set()
maxd = 1
for i in range(n):
prev = i
curr = nums[prev]
if prev not in visited:
d = 0
while prev != curr and prev... | array-nesting | Python super simple O(n), O(1) | byuns9334 | 0 | 171 | array nesting | 565 | 0.565 | Medium | 9,900 |
https://leetcode.com/problems/array-nesting/discuss/1439828/Python3-oror-Recursion%2BMemoization-oror-self-understandable | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
memo={}
def recurse(start,visited):
if start>=len(nums):
return 0
elif start in visited:
return 0
visited.add(start)
if start in memo:
retur... | array-nesting | Python3 || Recursion+Memoization || self-understandable | bug_buster | 0 | 40 | array nesting | 565 | 0.565 | Medium | 9,901 |
https://leetcode.com/problems/array-nesting/discuss/1439545/python3-simple-python-solution | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
## RC ##
## APPROACH : DFS ##
## LOGIC : If a cycle is formed, no matter which element we start with in that cycle, we end up with same cycle ##
visited = [False] * len(nums)
res = 1
def dfs(num,... | array-nesting | [python3] simple python solution | 101leetcode | 0 | 60 | array nesting | 565 | 0.565 | Medium | 9,902 |
https://leetcode.com/problems/array-nesting/discuss/1439263/Python3-Clean-Solution-O(N)-time-and-O(1)-space | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
_max = -1
for idx in range(len(nums)):
if nums[idx] == -1:
continue
cnt = 0
while nums[idx] != -1:
cnt += 1
old_idx = idx
i... | array-nesting | [Python3] Clean Solution - O(N) time and O(1) space | maosipov11 | 0 | 32 | array nesting | 565 | 0.565 | Medium | 9,903 |
https://leetcode.com/problems/array-nesting/discuss/1439036/Python-BFS-and-DFS-components-approach | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
def bfs(node):
global visited
queue = [node]
component = 0 # for storing the component size
while queue:
node = queue.pop(0)
visited[node] = True
co... | array-nesting | [Python] BFS and DFS components approach | mizan-ali | 0 | 40 | array nesting | 565 | 0.565 | Medium | 9,904 |
https://leetcode.com/problems/array-nesting/discuss/1439036/Python-BFS-and-DFS-components-approach | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
def dfs(node):
global visited
res = 1
visited[node] = True
if visited[nums[node]] == False:
res += dfs(nums[node])
return res
global visited
an... | array-nesting | [Python] BFS and DFS components approach | mizan-ali | 0 | 40 | array nesting | 565 | 0.565 | Medium | 9,905 |
https://leetcode.com/problems/array-nesting/discuss/1438872/Python3-Time-O(N)-and-space-O(N)-in-Python3-straight-forward-easy-to-understand | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
visited = set()
arr = []
n = len(nums)
if n == 1: # if only 1 element we return 1
return 1
x = 0
i = 0
res = 0
while i != n-1: # for visiting every element
... | array-nesting | Python3 Time O(N) and space O(N) in Python3 straight forward easy to understand | Shubham_Muramkar | 0 | 23 | array nesting | 565 | 0.565 | Medium | 9,906 |
https://leetcode.com/problems/array-nesting/discuss/1438303/PythonGo-O(-n-)-by-set-and-iteration-w-Comment | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
visited = set()
size = len(nums)
max_subset_size = 0
# check each index in input array
for i in range(size):
if nums[i] in visited:
... | array-nesting | Python/Go O( n ) by set and iteration [w/ Comment] | brianchiang_tw | 0 | 59 | array nesting | 565 | 0.565 | Medium | 9,907 |
https://leetcode.com/problems/array-nesting/discuss/1259865/Python-solution-using-recursion | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
def util(i, sequence):
value = nums[i]
if value in sequence:
return sequence
sequence.add(value)
return util(value, sequence)
maxi = 0
for i in range(len(nums))... | array-nesting | Python solution using recursion | okinasaru | 0 | 73 | array nesting | 565 | 0.565 | Medium | 9,908 |
https://leetcode.com/problems/array-nesting/discuss/1259865/Python-solution-using-recursion | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
memo = {}
def util(i, sequence):
if i in memo:
return memo[i]
value = nums[i]
if value in sequence:
return sequence
sequence.add(value)
mem... | array-nesting | Python solution using recursion | okinasaru | 0 | 73 | array nesting | 565 | 0.565 | Medium | 9,909 |
https://leetcode.com/problems/array-nesting/discuss/884055/Python3-dfs-O(N) | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
ans = 0
seen = [False]*len(nums)
for x in nums:
val = 0
while not seen[x]:
seen[x] = True
val += 1
x = nums[x]
ans = max(ans, val) #... | array-nesting | [Python3] dfs O(N) | ye15 | 0 | 85 | array nesting | 565 | 0.565 | Medium | 9,910 |
https://leetcode.com/problems/array-nesting/discuss/884055/Python3-dfs-O(N) | class Solution:
def arrayNesting(self, nums: List[int]) -> int:
ans = 0
for i in range(len(nums)):
cnt = 0
while nums[i] != -1:
cnt += 1
nums[i], i = -1, nums[i]
ans = max(ans, cnt)
return ans | array-nesting | [Python3] dfs O(N) | ye15 | 0 | 85 | array nesting | 565 | 0.565 | Medium | 9,911 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2046840/Python-Intuitive-%2B-Direct-for-Beginners-with-Illustrations | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
flatten = []
new_mat = []
for row in mat:
for num in row:
flatten.append(num)
if r * c != len(flatten): # when given parameters is NOT poss... | reshape-the-matrix | [Python] Intuitive + Direct for Beginners with Illustrations | ziaiz-zythoniz | 45 | 1,100 | reshape the matrix | 566 | 0.627 | Easy | 9,912 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1317320/Python-One-Line-Two-Line-Yield-Generator-Circular-Index-Numpy-with-explaination(556) | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
queue = [cell for row in mat for cell in row] if r*c==len(mat)*len(mat[0]) else []
return [[queue.pop(0) for _ in range(c)] for _ in range(r)] if queue else mat | reshape-the-matrix | Python - One Line, Two Line, Yield, Generator, Circular Index, Numpy - with explaination(#556) | abhira0 | 23 | 983 | reshape the matrix | 566 | 0.627 | Easy | 9,913 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1317320/Python-One-Line-Two-Line-Yield-Generator-Circular-Index-Numpy-with-explaination(556) | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if r*c!=len(mat)*(cols:=len(mat[0])): return mat
return [[mat[(i*c+j)//cols][(i*c+j)%cols] for j in range(c)] for i in range(r)] | reshape-the-matrix | Python - One Line, Two Line, Yield, Generator, Circular Index, Numpy - with explaination(#556) | abhira0 | 23 | 983 | reshape the matrix | 566 | 0.627 | Easy | 9,914 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1317320/Python-One-Line-Two-Line-Yield-Generator-Circular-Index-Numpy-with-explaination(556) | class Solution:
def getCell(self, mat):
for row in mat:
for cell in row:
yield cell
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
gen = self.getCell(mat)
return [[next(gen) for _ in range(c)] for _ in range(r)] if len(mat) *... | reshape-the-matrix | Python - One Line, Two Line, Yield, Generator, Circular Index, Numpy - with explaination(#556) | abhira0 | 23 | 983 | reshape the matrix | 566 | 0.627 | Easy | 9,915 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1317320/Python-One-Line-Two-Line-Yield-Generator-Circular-Index-Numpy-with-explaination(556) | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
gen = (cell for row in mat for cell in row)
return [[next(gen) for _ in range(c)] for _ in range(r)] if len(mat) * len(mat[0]) == r * c else mat | reshape-the-matrix | Python - One Line, Two Line, Yield, Generator, Circular Index, Numpy - with explaination(#556) | abhira0 | 23 | 983 | reshape the matrix | 566 | 0.627 | Easy | 9,916 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1555025/Easy-Python-Solution-or-Faster-than-99.3-(76-ms)-or-With-Comments | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
rw = len(mat)
cl = len(mat[0])
old = rw * cl
new = r * c
# checking if number of elements remains the same
if old != new:
return mat
old = [ i for e in ... | reshape-the-matrix | Easy Python Solution | Faster than 99.3% (76 ms) | With Comments | the_sky_high | 13 | 1,100 | reshape the matrix | 566 | 0.627 | Easy | 9,917 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2346903/EASY-PYTHON-SOLUTION-oror-FAST-oror-oror-beginner-friendly | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
flatten = []
new_mat = []
for row in mat:
for num in row:
flatten.append(num)
if r * c != len(flatten): # when given parameters is NOT poss... | reshape-the-matrix | EASY PYTHON SOLUTION || FAST ||✔✔ || beginner friendly | adithya_s_k | 5 | 230 | reshape the matrix | 566 | 0.627 | Easy | 9,918 |
https://leetcode.com/problems/reshape-the-matrix/discuss/480865/3-Python-sol.-based-on-list-comprehension-generator-and-index-transform-80%2B-With-explanation | class Solution:
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
ori_rows, ori_cols = len(nums), len(nums[0])
if ori_rows * ori_cols != r * c or (ori_rows, ori_cols) == (r, c):
# Quick response:
#
# mismatch of ... | reshape-the-matrix | 3 Python sol. based on list comprehension, generator, and index transform 80%+ [ With explanation ] | brianchiang_tw | 5 | 563 | reshape the matrix | 566 | 0.627 | Easy | 9,919 |
https://leetcode.com/problems/reshape-the-matrix/discuss/480865/3-Python-sol.-based-on-list-comprehension-generator-and-index-transform-80%2B-With-explanation | class Solution:
def element_generator( self, matrix ):
rows, cols = len( matrix), len( matrix[0])
for index_serial in range( rows*cols):
y = index_serial // cols
x = index_serial % cols
yield matrix[y][x]
def matrixReshap... | reshape-the-matrix | 3 Python sol. based on list comprehension, generator, and index transform 80%+ [ With explanation ] | brianchiang_tw | 5 | 563 | reshape the matrix | 566 | 0.627 | Easy | 9,920 |
https://leetcode.com/problems/reshape-the-matrix/discuss/480865/3-Python-sol.-based-on-list-comprehension-generator-and-index-transform-80%2B-With-explanation | class Solution:
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
ori_rows, ori_cols = len(nums), len(nums[0])
if ori_rows * ori_cols != r * c or (ori_rows, ori_cols) == (r, c):
# Quick response:
# mismatch of total number o... | reshape-the-matrix | 3 Python sol. based on list comprehension, generator, and index transform 80%+ [ With explanation ] | brianchiang_tw | 5 | 563 | reshape the matrix | 566 | 0.627 | Easy | 9,921 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1467350/Python3-easy-solution-O(n*m)-with-explanation-and-problem-solving-logic | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
# first step is quite obvious - check if transformation is possible
# check if rows*columns dimensions are same for og and transformed matrix
if len(mat)*len(mat[0]) != r*c:
... | reshape-the-matrix | Python3 easy solution O(n*m) with explanation and problem solving logic | ajinkya2021 | 4 | 273 | reshape the matrix | 566 | 0.627 | Easy | 9,922 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1089254/Python3-simple-solution | class Solution:
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
n = len(nums)
m = len(nums[0])
if n*m != r*c:
return nums
else:
l = []
res = []
for i in range(n):
l.extend(nums[i])
... | reshape-the-matrix | Python3 simple solution | EklavyaJoshi | 4 | 232 | reshape the matrix | 566 | 0.627 | Easy | 9,923 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2251623/Python-single-for-loop-or-Fast | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
cm = len(mat[0])
if r*c != len(mat)*cm:
return mat
out = [[] for i in range(r)]
for i in range(r*c):
out[i//c].append(mat[i//cm][i%cm])
return ou... | reshape-the-matrix | Python single for loop | Fast | gagan352 | 3 | 169 | reshape the matrix | 566 | 0.627 | Easy | 9,924 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1787625/python-3-or-Simple-Solution-or-93-lessser-memory | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
original = []
for i in mat:
for j in i:
original.append(j)
if r*c != len(original):
return mat
lst = []
for i in range(0,len(original),c):... | reshape-the-matrix | ✔python 3 | Simple Solution | 93% lessser memory | Coding_Tan3 | 3 | 254 | reshape the matrix | 566 | 0.627 | Easy | 9,925 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1319646/Python-Solution | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
n = len(mat)
m = len(mat[0])
if n * m != r * c:
return mat
new_mat = [[0] * c for _ in range(r)]
col = row = 0
for i in range(n):
for j i... | reshape-the-matrix | Python Solution | mariandanaila01 | 2 | 289 | reshape the matrix | 566 | 0.627 | Easy | 9,926 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1317282/Reshape-the-Matrix-Python-Easy-solution | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if mat:
if len(mat)*len(mat[0]) == r*c:
flat = [x for a in mat for x in a]
ans = [flat[i*c:(i +1)*c] for i in range(r)]
return ans
else:
... | reshape-the-matrix | Reshape the Matrix , Python , Easy solution | user8744WJ | 2 | 140 | reshape the matrix | 566 | 0.627 | Easy | 9,927 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1184044/Python-fast-and-pythonic | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
prime = [x for row in mat for x in row]
return [prime[x:x+c] for x in range(0, len(prime), c)] if r * c == len(prime) else mat | reshape-the-matrix | [Python] fast and pythonic | cruim | 2 | 223 | reshape the matrix | 566 | 0.627 | Easy | 9,928 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1156537/Easy-to-understand-Python-solution-or-O(N*M)-time-and-space | class Solution:
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
num_of_rows = len(nums)
num_of_columns = len(nums[0])
if num_of_rows * num_of_columns != r * c:
return nums
matrix = []
row = col = 0
for r... | reshape-the-matrix | Easy to understand Python solution | O(N*M) time and space | vanigupta20024 | 2 | 305 | reshape the matrix | 566 | 0.627 | Easy | 9,929 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2324188/Easy-Solution-with-Comments | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
# Make a new Array
arr = [[None]*c for i in range(r)]
# define row and col for new array
row = 0
col = 0
# taking the length of row and col of original mat for better understanding
l_col... | reshape-the-matrix | Easy Solution with Comments | nush29 | 1 | 68 | reshape the matrix | 566 | 0.627 | Easy | 9,930 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2248320/Simple-intuitive-approach-insert-into-next-available-cell-in-reshaped-matrix | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
# Return original input if the size does not match the size of the target dimensions
if r * c != len(mat) * len(mat[0]):
return mat
new_row = new_col = 0
reshaped = ... | reshape-the-matrix | Simple intuitive approach - insert into next available cell in reshaped matrix | graceiscoding | 1 | 29 | reshape the matrix | 566 | 0.627 | Easy | 9,931 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2089034/python-simple-solution-(Flattening-Slicing) | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
# Creat a flat matrix
flat_mat = [num for sublist in mat for num in sublist]
n = len(flat_mat)
# Check if reshape is invalid
if n%r:
return mat
k = n//r ... | reshape-the-matrix | python simple solution (Flattening, Slicing) | pe-mn | 1 | 79 | reshape the matrix | 566 | 0.627 | Easy | 9,932 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1968511/Python-oror-4-line-matrix-to-array-transformation | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
m, n = len(mat), len(mat[0])
if m * n != r * c: return mat
arr = [mat[row][col] for row in range(m) for col in range(n)]
return [arr[i:i + c] for i in range(0, r * c, c)] | reshape-the-matrix | Python || 4-line matrix to array transformation | gulugulugulugulu | 1 | 66 | reshape the matrix | 566 | 0.627 | Easy | 9,933 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1839404/Python-visualizing-the-approach | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
# count of rows & columns of initial matrix
row = len(mat)
col = len(mat[0])
newmat = []
# if the product of rows & cols of mat and the new matrix are not same then retu... | reshape-the-matrix | 🔥 [Python] visualizing the approach | MdKamrulShahin | 1 | 102 | reshape the matrix | 566 | 0.627 | Easy | 9,934 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1836818/4-Lines-Python-Solution-oror-75-Faster-oror-Memory-less-than-90 | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
m=len(mat) ; n=len(mat[0]) ; ans=[] ; M=list(chain(*mat))
if r*c!=m*n: return mat
for i in range(0,m*n,c): ans.append(M[i:i+c])
return ans | reshape-the-matrix | 4-Lines Python Solution || 75% Faster || Memory less than 90% | Taha-C | 1 | 66 | reshape the matrix | 566 | 0.627 | Easy | 9,935 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1821509/Easy-Python-using-generator | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
elements = []
for a in mat:
elements += a
if r * c == len(elements):
return [elements[n:n+c] for n in range(0, len(elements), c)]
else:
... | reshape-the-matrix | Easy Python using generator | Yuhan21 | 1 | 52 | reshape the matrix | 566 | 0.627 | Easy | 9,936 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1587051/Python-Easy-to-Understand | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
mat_len = 0
dupl = []
for x in mat:
mat_len += len(x)
for e in x:
dupl.append(e)
if r*c != mat_len:
return mat
... | reshape-the-matrix | Python Easy to Understand | mclovin286 | 1 | 143 | reshape the matrix | 566 | 0.627 | Easy | 9,937 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1502699/Python-solution-faster-than-80 | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
n,m = len(mat[0]),len(mat)
t = r*c
if n*m != t: return mat
output_arr = [[0 for _ in range(c)] for _ in range(r)]
row_num = 0
col_num = 0
for i in range(m):
... | reshape-the-matrix | Python solution faster than 80% | hrithikrawal4 | 1 | 210 | reshape the matrix | 566 | 0.627 | Easy | 9,938 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1495295/Python-or-Explained-or-Easy-to-Understand | class Solution:
def matrixReshape(self, nums, r, c):
m, n, count = len(nums), len(nums[0]), 0
if m*n != r*c: return nums
res = [[0] * c for _ in range(r)]
for i, j in product(range(m), range(n)):
res[count//c][count%c] = nums[i][j]
count += 1
ret... | reshape-the-matrix | ✅ Python | Explained | Easy to Understand | IamUday | 1 | 193 | reshape the matrix | 566 | 0.627 | Easy | 9,939 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1318124/Python3-via-iterator | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
m, n = len(mat), len(mat[0])
if m * n != r * c: return mat
ans = [[0]*c for _ in range(r)]
for i in range(m*n):
ans[i//c][i%c] = mat[i//n][i%n]
return ans | reshape-the-matrix | [Python3] via iterator | ye15 | 1 | 36 | reshape the matrix | 566 | 0.627 | Easy | 9,940 |
https://leetcode.com/problems/reshape-the-matrix/discuss/1318124/Python3-via-iterator | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
m, n = len(mat), len(mat[0])
if m * n != r * c: return mat
it = (x for row in mat for x in row)
return [[next(it) for _ in range(c)] for _ in range(r)] | reshape-the-matrix | [Python3] via iterator | ye15 | 1 | 36 | reshape the matrix | 566 | 0.627 | Easy | 9,941 |
https://leetcode.com/problems/reshape-the-matrix/discuss/353754/Solution-in-Python-3-(two-lines) | class Solution:
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
R = [i for r in nums for i in r]
return [R[i:i+c] for i in range(0,r*c,c)] if len(R) == r*c else nums | reshape-the-matrix | Solution in Python 3 (two lines) | junaidmansuri | 1 | 411 | reshape the matrix | 566 | 0.627 | Easy | 9,942 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2846294/python3-oror-beats-90%2B-oror-with-explanations | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
nrows, ncols = len(mat), len(mat[0])
if r * c != nrows * ncols: # when given parameters is NOT possible and legal
return mat
new_mat = [[]]
i1, i2 = 0, 0
for i in ... | reshape-the-matrix | python3 || beats 90+% || with explanations | Pavel_Kos | 0 | 1 | reshape the matrix | 566 | 0.627 | Easy | 9,943 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2838975/Python3-simple-code | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
m = len(mat)
n = len(mat[0])
reshape =[]
for k in range(r):
reshape.append([])
print(reshape)
if m*n != r*c:
return mat
else:
... | reshape-the-matrix | Python3 simple code | sukumaran1004 | 0 | 1 | reshape the matrix | 566 | 0.627 | Easy | 9,944 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2819700/Python-Simple-4-line-solution | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
temp = [i for j in mat for i in j]
if r * c != len(temp):
return mat
return [temp[row * c : row * c + c] for row in range(r)] | reshape-the-matrix | Python Simple 4 line solution | Insatanic | 0 | 4 | reshape the matrix | 566 | 0.627 | Easy | 9,945 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2814150/TC%3A-91.11-Python-simple-solution | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if r * c != len(mat) * len(mat[0]):
return mat
flatten = []
new_mat = []
for row in mat:
for num in row:
flatten.append(num)
... | reshape-the-matrix | 😎 TC: 91.11% Python simple solution | Pragadeeshwaran_Pasupathi | 0 | 4 | reshape the matrix | 566 | 0.627 | Easy | 9,946 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2813317/Simple-solution-beats-58.91-on-runtime | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
"""
Condition for possible reshaping: m*n == r*c, where m and n are sizes of mat
If condition is filed or the m == r and n == c, return origin matrix.
Create new matrix with r and c siz... | reshape-the-matrix | Simple solution beats 58.91% on runtime | denisOgr | 0 | 2 | reshape the matrix | 566 | 0.627 | Easy | 9,947 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2811286/Straight-forward-python-solution | class Solution:
def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
if len(nums) * len(nums[0]) != r * c:
return nums
ans = [[]]
lenlast = 0
for i in range(len(nums)):
for j in range(len(nums[0])):
... | reshape-the-matrix | Straight forward python solution | thunder34 | 0 | 4 | reshape the matrix | 566 | 0.627 | Easy | 9,948 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2794057/Easy-Approach-or-Python | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if len(mat) * len(mat[0]) != r*c:
return mat
ans = []
row = []
for rows in range(len(mat)):
for cols in range(len(mat[0])):
row.append(m... | reshape-the-matrix | Easy Approach | Python | user3108Vs | 0 | 2 | reshape the matrix | 566 | 0.627 | Easy | 9,949 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2792134/1st-attempt-Flatten-and-rearrange-the-elements | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
counter = 0
for i in mat:
counter += len(i)
if counter == r*c:
new_l = [x for l in mat for x in l]
new_mat = []
for row_index in range(r):
... | reshape-the-matrix | 1st attempt - Flatten and rearrange the elements | abhinandhjp20 | 0 | 2 | reshape the matrix | 566 | 0.627 | Easy | 9,950 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2771994/Very-easy-to-understand-python-solution | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
row = len(mat)
column = len(mat[0])
if row * column != r*c: # Not a valid matrix requirement
return mat
flatten_lst = [item for sub in mat for item in sub]
if r == 1: #list of list of the flatten list
... | reshape-the-matrix | Very easy to understand python solution | AnuragOn2 | 0 | 8 | reshape the matrix | 566 | 0.627 | Easy | 9,951 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2757720/Python-beats-85-Compute.-(Need-Time-%2B-Space-Complexity-please-still-learning!) | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
# r = number of elements
# c = number of sub-elements
rr = len(mat)
cc = len(mat[0])
if r * c != rr * cc: return mat
else:
res = []
rowCount = 0
... | reshape-the-matrix | Python, beats 85% Compute. (Need Time + Space Complexity, please still learning!) | R-Dub | 0 | 4 | reshape the matrix | 566 | 0.627 | Easy | 9,952 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2756997/simple-and-easy-solution-with-python3 | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
array = []
for i in mat:
array.extend(i)
# all element convert in single array.
if r*c != len(array): # if given size is not possible than return same matrix.
... | reshape-the-matrix | simple and easy solution with python3 | amangupta3073 | 0 | 1 | reshape the matrix | 566 | 0.627 | Easy | 9,953 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2747752/Reshape-The-Matrix-in-Python | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
flat_mat = [j for i in mat for j in i]
if r * c != len(flat_mat):
return mat
res = [flat_mat[(i * c) :c * (i +1)] for i in range(r)]
return res | reshape-the-matrix | Reshape The Matrix in Python | jashii96 | 0 | 2 | reshape the matrix | 566 | 0.627 | Easy | 9,954 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2746631/Simple-Python-Solution | class Solution:
def getLen(self,l):
a=len(l)
b=len(l[0])
return a*b
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
s=Solution()
if(s.getLen(mat)!=r*c):
return mat
temp=[]
for i in mat:
temp+=i
... | reshape-the-matrix | Simple Python Solution | icebear190602 | 0 | 2 | reshape the matrix | 566 | 0.627 | Easy | 9,955 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2741691/Smple-python-solution | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if len(mat) * len(mat[0]) != r * c:
return mat
result = []
tmp = []
for row in range(len(mat)):
for col in range(len(mat[0])):
tmp.append... | reshape-the-matrix | Smple python solution | Delacrua | 0 | 3 | reshape the matrix | 566 | 0.627 | Easy | 9,956 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2667879/Python-or-Easy-solution-(-) | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if len(mat)*len(mat[0]) != r*c:
return mat
new_mat = []
t, p = 0, 0
for i in range(r):
carry = []
for j in range(c):
carry.append(mat[... | reshape-the-matrix | Python | Easy solution ( ͡° ͜ʖ ͡°) | LordVader1 | 0 | 29 | reshape the matrix | 566 | 0.627 | Easy | 9,957 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2642585/Python-or-2-lines-Solution-using-python-generators | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if r * c != len(mat) * len(mat[0]): return mat
def get_values(mat):
for row in mat:
for val in row:
yield val
value ... | reshape-the-matrix | Python | 2-lines Solution using python generators | ahmadheshamzaki | 0 | 139 | reshape the matrix | 566 | 0.627 | Easy | 9,958 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2642585/Python-or-2-lines-Solution-using-python-generators | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
value = (val for row in mat for val in row)
return mat if r * c != len(mat) * len(mat[0]) else [[next(value) for i in range(c)] for j in range(r)] | reshape-the-matrix | Python | 2-lines Solution using python generators | ahmadheshamzaki | 0 | 139 | reshape the matrix | 566 | 0.627 | Easy | 9,959 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2609891/SIMPLE-PYTHON3-SOLUTION-easy-approach | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
i = []
o = 0
final = []
len_mat = 0
for j in range(0,len(mat)):
len_mat += len(mat[j])
if len_mat > r*c or len_mat < r*c:
return mat
else:... | reshape-the-matrix | ✅✔ SIMPLE PYTHON3 SOLUTION ✅✔ easy-approach | rajukommula | 0 | 68 | reshape the matrix | 566 | 0.627 | Easy | 9,960 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2592469/Python3-two-easy-solutions-with-explanation | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
if len(mat) * len(mat[0]) != r * c: #if reshape not possible return original matrix
return mat
output = [[0 for i in range(c)] for i in range(r)]
idx = 0
while idx < r * c:
... | reshape-the-matrix | Python3 two easy solutions with explanation | khushie45 | 0 | 88 | reshape the matrix | 566 | 0.627 | Easy | 9,961 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2521555/Python-or-Simple-solution-or-Beats-95-or-Explained | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
rows, cols = len(mat), len(mat[0])
# if the requested matrix dimensions are illegal, return the matrix itself
# also, if the requested sizes are the same as the original, return the mat... | reshape-the-matrix | Python | Simple solution | Beats 95% | Explained | anon82214 | 0 | 61 | reshape the matrix | 566 | 0.627 | Easy | 9,962 |
https://leetcode.com/problems/reshape-the-matrix/discuss/2508917/Python-Solution-or-Explained-or-Easy-or-Beats-94.3 | class Solution:
def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]:
newList = []
#convert to 1-D list
for i in mat:
for j in i:
newList.append(j)
elements = len(newList) #total no. of elements
resMat ... | reshape-the-matrix | Python Solution | Explained | Easy | Beats 94.3%✅ | sharwariakre | 0 | 70 | reshape the matrix | 566 | 0.627 | Easy | 9,963 |
https://leetcode.com/problems/permutation-in-string/discuss/1476884/Python-Sliding-Window-and-Dictionary.-Easy-to-understand | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
window = len(s1)
s1_c = Counter(s1)
for i in range(len(s2)-window+1):
s2_c = Counter(s2[i:i+window])
if s2_c == s1_c:
return True
return False | permutation-in-string | Python Sliding Window and Dictionary. Easy to understand | ParthitPatel | 29 | 3,000 | permutation in string | 567 | 0.437 | Medium | 9,964 |
https://leetcode.com/problems/permutation-in-string/discuss/1763595/Python3-or-faster-Solution-with-Hash-map-simple-approach-or-O(n) | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
h1=Counter(s1)
n=len(s1)
for i in range(len(s2)-len(s1)+1):
h2 = Counter(s2[i:i+n])
if h1==h2:
return True
return False | permutation-in-string | Python3 | faster Solution with Hash map, simple approach | O(n) | Anilchouhan181 | 10 | 946 | permutation in string | 567 | 0.437 | Medium | 9,965 |
https://leetcode.com/problems/permutation-in-string/discuss/1438629/Simple-Python-Solution-Sliding-Window | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
s1freq = [0]*26
s2freq = [0]*26
for i in range(len(s1)):
s1freq[ord(s1[i])-97] += 1
s2freq[ord(s2[i])-97] += 1
... | permutation-in-string | Simple Python Solution - Sliding Window | lokeshsenthilkumar | 7 | 1,100 | permutation in string | 567 | 0.437 | Medium | 9,966 |
https://leetcode.com/problems/permutation-in-string/discuss/638772/hash()-99 | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
s1h=0
s2h=0
if len(s2)<len(s1):
return False
for i in s1:
s1h+=hash(i)
for i in range(len(s1)):
s2h+=hash(s2[i])
if s1h==s2h:
return True
if len... | permutation-in-string | hash() 99% | kisdown | 5 | 1,000 | permutation in string | 567 | 0.437 | Medium | 9,967 |
https://leetcode.com/problems/permutation-in-string/discuss/638630/Python-O(n)-by-sliding-window-and-hash.-90-w-Visualization | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
size_1, size_2 = len(s1), len(s2)
if ( size_1 > size_2 ) or ( size_1 * size_2 == 0 ):
# Quick rejection for oversize pattern or empty input string
return False
... | permutation-in-string | Python O(n) by sliding window and hash. 90% [w/ Visualization] | brianchiang_tw | 4 | 688 | permutation in string | 567 | 0.437 | Medium | 9,968 |
https://leetcode.com/problems/permutation-in-string/discuss/2221058/Python-Sliding-Window-Time-O(N-%2B-M)-or-Space-O(1)-Explained | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
charFreq = {} # Keep track of all characters in s1 and their frequency
for char in s1:
if char not in charFreq:
charFreq[char] = 0
charFreq[char] += 1
matched = 0 # Keep track... | permutation-in-string | [Python] Sliding Window Time O(N + M) | Space O(1) Explained | Symbolistic | 3 | 226 | permutation in string | 567 | 0.437 | Medium | 9,969 |
https://leetcode.com/problems/permutation-in-string/discuss/1682180/Python-Counter-%2B-Sliding-Window-(not-time-efficient-but-lesser-space-than-96.03) | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
window_size = len(s1)
s1_check = Counter(s1)
for i in range(len(s2)):
if Counter(s2[i:window_size + i]) == s1_check:
return... | permutation-in-string | Python Counter + Sliding Window (not time efficient but lesser space than 96.03%) | aadishah1 | 3 | 204 | permutation in string | 567 | 0.437 | Medium | 9,970 |
https://leetcode.com/problems/permutation-in-string/discuss/2801538/Python-oror-Easy-oror-Sliding-Window-%2B-Dictionary-oror-O(N)-Solution | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
n1,n2=len(s1),len(s2)
d1=Counter(s1)
d2=Counter(s2[:n1-1])
j=0
for i in range(n1-1,n2):
d2[s2[i]]+=1
if d1==d2:
return True
d2[s2[j]]-=1
if d2[s... | permutation-in-string | Python || Easy || Sliding Window + Dictionary || O(N) Solution | DareDevil_007 | 2 | 178 | permutation in string | 567 | 0.437 | Medium | 9,971 |
https://leetcode.com/problems/permutation-in-string/discuss/2403459/C%2B%2BPython-O(N)-Solution-with-fast-and-optimized-approach | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
window = len(s1)
s1_c = Counter(s1)
for i in range(len(s2)-window+1):
s2_c = Counter(s2[i:i+window])
if s2_c == s1_c:
return True
return False | permutation-in-string | C++/Python O(N) Solution with fast and optimized approach | arpit3043 | 2 | 160 | permutation in string | 567 | 0.437 | Medium | 9,972 |
https://leetcode.com/problems/permutation-in-string/discuss/2396343/Python-HashMap-Solution | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
d1 = {}
for i in s1:
if i in d1:
d1[i] += 1
else:
d1[i] = 1
win = len(s1)
for i in range(len(s2)... | permutation-in-string | Python HashMap Solution | joelkurien | 2 | 167 | permutation in string | 567 | 0.437 | Medium | 9,973 |
https://leetcode.com/problems/permutation-in-string/discuss/1690363/48-ms-faster-than-99.59-Pythonic-solution-with-explanation-(not-over-engineered) | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
d_s = {char:0 for char in ascii_lowercase}
for char in s1:
d_s[char]+= 1
d_window = {char:0 for char in ascii_lowercase}
for char i... | permutation-in-string | 48 ms, faster than 99.59% Pythonic solution with explanation (not over engineered) | dnz0x | 2 | 251 | permutation in string | 567 | 0.437 | Medium | 9,974 |
https://leetcode.com/problems/permutation-in-string/discuss/2573573/Python3-or-97-faster-or-Explained-step-by-step | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
ls1 = len(s1)
s1l = {}
s2l = {}
# pointer to point the character that should be deleted or remove one from its count
pointer = 0
# insert all characters in a dict for s1 {'char'->str: count->int}
for char in s1:
s1l[char] = 1 + s1l... | permutation-in-string | Python3 | 97% faster | Explained step by step | abdoohossamm | 1 | 93 | permutation in string | 567 | 0.437 | Medium | 9,975 |
https://leetcode.com/problems/permutation-in-string/discuss/2373331/5-Lines-Python-Solution-or-Easy-to-Understand | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
count_s2=Counter(s1)
for i in range(len(s2)-len(s1)+1):
if Counter(s2[i:i+len(s1)])-count_s2=={}:
return True
return False | permutation-in-string | 5 Lines Python Solution | Easy to Understand | dhruva3223 | 1 | 143 | permutation in string | 567 | 0.437 | Medium | 9,976 |
https://leetcode.com/problems/permutation-in-string/discuss/2265685/simply-python-sliding-window | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
counter, s1len = Counter(s1), len(s1)
for i in range(len(s2)):
if s2[i] in counter:
counter[s2[i]] -= 1
if i >= s1len and s2[i-s1len] in counter:
counter[s2[i-s1len]] += 1
... | permutation-in-string | simply python sliding window | gasohel336 | 1 | 142 | permutation in string | 567 | 0.437 | Medium | 9,977 |
https://leetcode.com/problems/permutation-in-string/discuss/2136258/Python-Counter-sliding-window | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s2) < len(s1):
return False
n1 = len(s1)
s1 = Counter(s1)
sub = Counter(s2[:n1])
if sub == s1:
return True
for i in range(n1, len(s2)):
sub[s2[... | permutation-in-string | Python, Counter sliding window | blue_sky5 | 1 | 52 | permutation in string | 567 | 0.437 | Medium | 9,978 |
https://leetcode.com/problems/permutation-in-string/discuss/1763011/PYTHON3-or-EASY-SOLUTION-%2B-CONCEPT-or | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2) : return False
count1 = collections.Counter(s1)
count2 = collections.Counter(s2[:len(s1)])
if count2 == count1 : return True
l = 0
for r in range(len(s1... | permutation-in-string | PYTHON3 | EASY SOLUTION + CONCEPT | | rohitkhairnar | 1 | 230 | permutation in string | 567 | 0.437 | Medium | 9,979 |
https://leetcode.com/problems/permutation-in-string/discuss/1763011/PYTHON3-or-EASY-SOLUTION-%2B-CONCEPT-or | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2) : return False
count1 = {}
count2 = {}
for i in s1 : # storing the count of chars in s1 in dic
if i in count1 :
co... | permutation-in-string | PYTHON3 | EASY SOLUTION + CONCEPT | | rohitkhairnar | 1 | 230 | permutation in string | 567 | 0.437 | Medium | 9,980 |
https://leetcode.com/problems/permutation-in-string/discuss/1762730/Python3-or-Super-Optimised-Code-or-Sliding-Window-or-Beats-99 | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s2)<len(s1):
return False
dic1,dic2,start={},{},0
for i in range(len(s1)):
if s1[i] in dic1:
dic1[s1[i]]+=1
else:
dic1[s1[i]]=1
if s2[i] ... | permutation-in-string | Python3 | Super Optimised Code | Sliding Window | Beats 99% | RickSanchez101 | 1 | 39 | permutation in string | 567 | 0.437 | Medium | 9,981 |
https://leetcode.com/problems/permutation-in-string/discuss/1762215/Sliding-window-solution-using-Python | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
hash_val = [0]*26
for s1_val in s1:
hash_val[ord(s1_val)-ord('a')] +=1
for i in range(len(s1)):
hash_val[ord(s2[i]... | permutation-in-string | Sliding window solution using Python | manojkumarmanusai | 1 | 106 | permutation in string | 567 | 0.437 | Medium | 9,982 |
https://leetcode.com/problems/permutation-in-string/discuss/1762059/Python-Sliding-Window-Solution-or-93-Faster-or-Easy-Solution | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
# Edge Condition
if len(s1) > len(s2):
return False
n1 = len(s1)
# Hashmap for s1
c1 = Counter(s1)
for i in range(len(s2)-n1+1):
if Counter(s2[i:i+n1]) == c1:
... | permutation-in-string | ✔️ Python Sliding Window Solution | 93% Faster | Easy Solution | pniraj657 | 1 | 263 | permutation in string | 567 | 0.437 | Medium | 9,983 |
https://leetcode.com/problems/permutation-in-string/discuss/1688661/Python.-Short-simple-fast. | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
c = {}
for ch in s1:
c[ch] = c.get(ch, 0) + 1
hm = {}
sublen = 0
for i, ch in enumerate(s2):
hm[ch] = hm.get(ch, 0) + 1
while hm[ch]... | permutation-in-string | Python. Short, simple, fast. | MihailP | 1 | 165 | permutation in string | 567 | 0.437 | Medium | 9,984 |
https://leetcode.com/problems/permutation-in-string/discuss/1517497/Python3-Sliding-window-2-lines | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
m, s = len(s1), sorted(s1)
return any(sorted(s2[i:i+m]) == s for i in range(len(s2)-m+1)) | permutation-in-string | [Python3] Sliding window, 2 lines | lapped | 1 | 162 | permutation in string | 567 | 0.437 | Medium | 9,985 |
https://leetcode.com/problems/permutation-in-string/discuss/1466478/Easy-Python-solution-using-dict | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
dct1 = {}
for i in s1:
dct1[i] = dct1.get(i,0)+1
dct2 = {}
for j in s2[:len(s1)]:
dct2[j] = dct2.get(j,0)+1
if dct1 == dct2:
return True
i = 1
while i < le... | permutation-in-string | Easy Python solution using dict | ce17b127 | 1 | 121 | permutation in string | 567 | 0.437 | Medium | 9,986 |
https://leetcode.com/problems/permutation-in-string/discuss/1243695/Python%3A-Using-Solution-from-Q-242.-Valid-anagram | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
def isPermute(s,t):
if len(s) != len(t):
return False
if s == t:
return True
from collections import Counter
s = Counter(s)
t = Counter(t)
... | permutation-in-string | Python: Using Solution from Q 242. Valid anagram | mehrotrasan16 | 1 | 266 | permutation in string | 567 | 0.437 | Medium | 9,987 |
https://leetcode.com/problems/permutation-in-string/discuss/639894/JavaPython3-freq-table | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
f1, f2, = [0]*26, [0]*26
for c in s1: f1[ord(c)-97] += 1
for i in range(len(s2)):
f2[ord(s2[i])-97] += 1
if i >= len(s1): f2[ord(s2[i-len(s1)])-97] -= 1
if i >= len(s... | permutation-in-string | [Java/Python3] freq table | ye15 | 1 | 64 | permutation in string | 567 | 0.437 | Medium | 9,988 |
https://leetcode.com/problems/permutation-in-string/discuss/639894/JavaPython3-freq-table | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
freq = Counter(s1)
for i, x in enumerate(s2):
freq[x] -= 1
if i >= len(s1): freq[s2[i-len(s1)]] += 1
if all(v == 0 for v in freq.values()): return True
return False | permutation-in-string | [Java/Python3] freq table | ye15 | 1 | 64 | permutation in string | 567 | 0.437 | Medium | 9,989 |
https://leetcode.com/problems/permutation-in-string/discuss/2844300/Python-or-Sliding-Window-or-Easiest-to-understand-or-O(n)-time-or-O(1)-space | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
count, count2 = Counter(s1), {}
l = 0
for r in range(len(s2)):
if r - l > len(s1) - 1:
count2[s2[l]] -= 1
l += 1
count2[s2[r]] = count2.get(s2[r],... | permutation-in-string | Python | Sliding Window | Easiest to understand | O(n) time | O(1) space | jasontogoogle | 0 | 4 | permutation in string | 567 | 0.437 | Medium | 9,990 |
https://leetcode.com/problems/permutation-in-string/discuss/2789971/8-line-solution-but-What-the-time-complexity-for-this-one | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
hashmap1 = Counter(s1)
l, r = 0, len(s1)
while r < len(s2) + 1:
if hashmap1 == Counter(s2[l:r]):
return True
l += 1
r += 1
return False | permutation-in-string | 8 line solution but What the time complexity for this one? | anishsubedi5566 | 0 | 1 | permutation in string | 567 | 0.437 | Medium | 9,991 |
https://leetcode.com/problems/permutation-in-string/discuss/2789966/9-line-solution-but-What-the-time-complexity-for-this-one | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
hashmap1 = Counter(s1)
hashmap2 = {}
l, r = 0, len(s1)
while r < len(s2) + 1:
if hashmap1 == Counter(s2[l:r]):
return True
l += 1
r += 1
... | permutation-in-string | 9 line solution but What the time complexity for this one? | anishsubedi5566 | 0 | 2 | permutation in string | 567 | 0.437 | Medium | 9,992 |
https://leetcode.com/problems/permutation-in-string/discuss/2789964/What-the-time-complexity-for-this-one | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
hashmap1 = Counter(s1)
hashmap2 = {}
l, r = 0, len(s1)
while r < len(s2) + 1:
if hashmap1 == Counter(s2[l:r]):
return True
l += 1
r += 1
... | permutation-in-string | What the time complexity for this one? | anishsubedi5566 | 0 | 1 | permutation in string | 567 | 0.437 | Medium | 9,993 |
https://leetcode.com/problems/permutation-in-string/discuss/2785782/Short-but-slow-solution | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
alpha_chars = set(s1)
for i in range((len(s2)-len(s1))+1):
if all([s1.count(letter) == s2[i:i+len(s1)].count(letter) for letter in alpha_chars]):
return True
return False | permutation-in-string | Short but slow solution | josephh27 | 0 | 2 | permutation in string | 567 | 0.437 | Medium | 9,994 |
https://leetcode.com/problems/permutation-in-string/discuss/2785574/Easy-Python-solution | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
l1=[0]*26
l2=[0]*26
if(len(s1)>len(s2)):
return False
for i in range(len(s1)):
l1[ord(s1[i])-ord('a')]+=1
l2[ord(s2[i])-ord('a')]+=1
if(l1==l2):
... | permutation-in-string | Easy Python solution | mehtadeepparesh | 0 | 1 | permutation in string | 567 | 0.437 | Medium | 9,995 |
https://leetcode.com/problems/permutation-in-string/discuss/2752593/Python-Sliding-Window-Solution | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
'''
create dictionary for s1
'''
s1_dict = {}
for i in range(len(s1)):
if s1[i] in s1_dict:
s1_dict[s1[i]] += 1
else:
s1_dict[s1[i]] =... | permutation-in-string | Python Sliding Window Solution | DietCoke777 | 0 | 7 | permutation in string | 567 | 0.437 | Medium | 9,996 |
https://leetcode.com/problems/permutation-in-string/discuss/2747243/Python-using-hashmap | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
c1 = collections.Counter(s1)
for i in range(len(s2)-len(s1)+1):
c2 = collections.Counter(s2[i:i+len(s1)])
if c1 == c2:
return True
else:
return False | permutation-in-string | Python using hashmap | leetcodesquad | 0 | 2 | permutation in string | 567 | 0.437 | Medium | 9,997 |
https://leetcode.com/problems/permutation-in-string/discuss/2743746/Python3-clean-and-well-documented. | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
# Length of s1 and s2
s1_len = len(s1)
s2_len = len(s2)
if s1_len > s2_len:
return False
# Hash table of s1
s1_hash: dict = {}
# Hash table of sliding window
sw_hash: d... | permutation-in-string | Python3, clean and well documented. | jeffreycashion | 0 | 1 | permutation in string | 567 | 0.437 | Medium | 9,998 |
https://leetcode.com/problems/permutation-in-string/discuss/2723969/python-3-or-sliding-window-or-T%3AO(1)-S%3AO(1) | class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
s1Counter, s2Counter = [0 for i in range(26)], [0 for i in range(26)]
for i in s1:
s1Counter[ord(i) - ord("a")] += 1
for idx, c in enumerate(s2):
if idx >= len(s1):
s2Counter[ord(s2[id... | permutation-in-string | python 3 | sliding window | T:O(1) S:O(1) | ivylu888 | 0 | 6 | permutation in string | 567 | 0.437 | Medium | 9,999 |
Subsets and Splits
Top 2 Solutions by Upvotes
Identifies the top 2 highest upvoted Python solutions for each problem, providing insight into popular approaches.