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]
Tags: Math

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]
Tags: Math

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]
Tags: String Stack

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]
Tags: String Stack