```python class Solution: def findAnagrams(self, s: str, p: str) -> List[int]: # time O(S+P) space O(1) cntP = {} cntS = {} r = 0 res = [] for pp in p: if pp not in cntP: cntP[pp] = 1 else: cntP[pp] += 1 # [l,r] is current window for...
[Read More]
948. Bag of Tokens
class Solution: def bagOfTokensScore(self, tokens: List[int], P: int) -> int: # time O(NlgN) space O(N) # main idea: sort , then buy at the cheapest and sell at the most expensive using queue tokens.sort() q = collections.deque(tokens) cur = 0 res = 0 # continue condition: when there is non-zero...
[Read More]
456. 132 Pattern
Stack One Pass O(N) space O(N)
[Read More]
Lc1546
Prefix Sum + HashSet() main question : class Solution: def maxNonOverlapping(self, nums: List[int], target: int) -> int: seen = {0} size = len(nums) cnt = 0 preSum = 0 for num in nums: preSum += num if preSum - target in seen: cnt += 1 seen = {0} preSum =...
[Read More]
14. Longest Common Prefix
```python
time O(N) space O(1)
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
l = zip(*strs)
res = ‘’
for tup in l:
if len(set(tup)) == 1:
res += tup[0]
else:
break
return res
[Read More]