class Solution: def lengthOfLongestSubstring(self, s: str) -> int: l,r = 0,0 d = {} res = 0 while r < len(s): if s[r] not in d: d[s[r]] = 1 else: d[s[r]] += 1 while d[s[r]] > 1: d[s[l]] -= 1 l += 1 res = max(res,r-l+1) r += 1 return...
[Read More]
LC1329 Sort the Matrix Diagonally
```python
“””
main idea: get the diagonal val based on diff of coordinate, sort by diff in descending order, then put them back
[Read More]
LC49 Group Anagrams
from collections import defaultdict class Solution: # time O(NWlgW), where N is length of strs, space O(NW) def groupAnagrams(self, strs: List[str]) -> List[List[str]]: d = defaultdict(list) res = [] for str in strs: k = tuple(sorted(str))# or "".join(sorted(str)) d[k].append(str)# note here can't use list ,set as key since they are...
[Read More]
LC767 Reorganize String
```python
“””
main idea: count the char in the string, check if max_cnt char has reached (len(S)+1)//2, if yes return “”; otherwise, insert in odd position and even position seperately
[Read More]
LC387 First Unique Character in a String
class Solution: def firstUniqChar(self, s: str) -> int: # main idea: dictionary.items() return in insertion order # dictionary k:char, v: cnt # find the first k where v == 1 # time O(N) space O(1) <= O(A) since Character has at most 26 types if not s: return -1 d...
[Read More]