class Solution: def minWindow(self, s: str, t: str) -> str: # time O(S+T) space O(T) # step1: initialize l, minL, minLen, cnt_s, cnt_t. where cnt_s record valid ch in s for t. # step2: record freq for each char in t d = {} for c in t: d[c] =...
[Read More]
443. String Compression
```python class Solution: def compress(self, chars: List[str]) -> int: l = r = 0 size = len(chars) cnt = 1 for r in range(1,size+1): if r < size and chars[r] == chars[r-1]: cnt += 1 else: chars[l] = chars[r-1] l += 1 if cnt > 1: for x in str(cnt):...
[Read More]
1492. The kth Factor of n
```python class Solution: def kthFactor(self, n: int, k: int) -> int: “”” tc O(sqrt(N)) sc O(sqrt(N)) 1. set i upper bound sqrt(n) 2. keep incrementing i up to upperbound 2.1 check if i is factor of n if yes, check if n//i is same as i : if divisor is...
[Read More]
772. Basic Calculator III
```python time(N^2) space O(N) class Solution: def calculate(self, s: str) -> int: def helper(s): st = [] num = 0 sign = ‘+’ while s : c = s.pop(0) # 1. regular count number if c.isdigit(): num = 10*num + int(c) # 2. check if ( if c == ‘(‘:...
[Read More]
227. Basic Calculator II
```python “”” main idea: each number is constrained by its operator right before it. note first number’s operator by default is +; In addition, there are 4 cases: 1. number 2. operator 3. space 4. end of the loop “”” class Solution: def calculate(self, s: str) -> int: # time...
[Read More]