class Solution: def getPermutation(self, n: int, k: int) -> str: # time O(N^2) space O(N) # edge case n=0 if n == 0: return "" used = [False for _ in range(n+1)] path = [] # cache factorial number factorial = [1 for _ in range(n+1)] for i in range(2,n+1):...
[Read More]
760. Find Anagram Mappings
class Solution:
def anagramMappings(self, A: List[int], B: List[int]) -> List[int]:
d = {}
res = []
for idx,b in enumerate(B):
if b not in d:
d[b] = idx
for a in A:
res.append(d[a])
return res
[Read More]
525. Contiguous Array
class Solution: def findMaxLength(self, nums: List[int]) -> int: # map idx: cnt, only record first time cnt appear with earliest idx # edge case: if from beginning the subarrary is already 0/1 switch changing (cnt= 0), max_len = current idx - (-1) d = {0:-1} n= len(nums) max_len = 0...
[Read More]
1003. Check If Word Is Valid After Substitutions
Main idea: each time find ch == ‘c’ check top of stack of they are ‘a’,’b’ in order, then pop them off time O(N) space O(N) class Solution: def isValid(self, s: str) -> bool: # checki if s is potential candidate counter = collections.Counter(s) if len(set(counter.values())) != 1: return False...
[Read More]
73. Set Matrix Zeroes
```python time O(N+M) space O(MN) class Solution: def setZeroes(self, matrix: List[List[int]]) -> None: “”” Do not return anything, modify matrix in-place instead. “”” seen = set() q = [] row = len(matrix) col = len(matrix[0]) for i in range(row): for j in range(col): if matrix[i][j] == 0: q.append((i,j)) seen.add((i,j)) for...
[Read More]