Two pass ```python class Solution: def longestConsecutive(self, nums: List[int]) -> int: # time O(N) space O(N) # step1: create a hashset and put num into set # step2: loop through nums, check each num’s neighbour if left neighbour not in the set(means num may be beginning of the sequence) #...
[Read More]
859. Buddy Strings
```python class Solution: time O(N) space O(N) def buddyStrings(self, A: str, B: str) -> bool: #early termination: len_a != len_b if len(A) != len(B): return False # case1: A == B if A == B and len(A) > len(set(A)): print('here') return True # case2: A != B, be careful there...
[Read More]
449. Serialize and Deserialize BST
be careful with local variable scope ```python Definition for a binary tree node. class TreeNode: def init(self, x): self.val = x self.left = None self.right = None ””” main idea: for serialize , do preorder; for deserilize, use queue to update upper bound and lower bound to detemine if current...
[Read More]
297. Serialize and Deserialize Binary Tree
```python
Definition for a binary tree node.
class TreeNode(object):
def init(self, x):
self.val = x
self.left = None
self.right = None
[Read More]
316. Remove Duplicate Letters
lexical order
strings are compared from beginning to end. it’s not equal to sorted order.e.g. abc < acdb
==> smallest lexical order ~ get the smallest characters as early as possible
[Read More]