class Solution: def numSubseq(self, A: List[int], target: int) -> int: """ tc O(NlgN) sc O(N) can optimize to O(1) by using math.pow(2,diff,M) main idea: two ptr 1. sort 2. preprocessing: for each A[i], find max A[j] s.t. A[i] + A[j] <= target ====> for subarray A[i+1] ~ A[j], can choose...
[Read More]
1234. Replace the Substring for Balanced String
```python class Solution: def balancedString(self, s: str) -> int: “”” tc O(N) sc O(1) since only cnt 4 char QWER note,only 4 types of char main idea: two pointer— instead of checking if substring within the window is balanced, we check substring count outside window if by adding them into...
[Read More]
1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers
```python class Solution: def numTriplets(self, nums1: List[int], nums2: List[int]) -> int: “”” Brute Force tc O(NM) sc O(max(N,M)) variation of two sum? cache nums1 and nums2’s item^2 value seperately and loop x comparing with target y^2 and record expected z = y^2/x “”” A = combinations(nums1,2) B = combinations(nums2,2) A1...
[Read More]
1652. Defuse the Bomb
Brute Force
```python
“””
tc O(N) sc O(N)
[Read More]
266. Palindrome Permutation
```python class Solution: def canPermutePalindrome(self, s: str) -> bool: “”” main idea: check if there is at most one char has odd cnt tc O(N) sc O(26) ~ O(1) “”” cnt = {} for c in s: if c not in cnt: cnt[c] = 1 else: cnt[c] += 1 res...
[Read More]