LC804. Unique Morse Code Words

```python class Solution: def uniqueMorseRepresentations(self, words: List[str]) -> int: “”” tc O(NWM) W: len of word, N number of words, M: len of morse code """ res = set() d = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] for w in words: tmp = '' # optimize:can use arr to cache first and join in one... [Read More]
Tags: String

LC1559. Detect Cycles in 2D Grid

```python class Solution: def containsCycle(self, grid: List[List[str]]) -> bool: “"”tc O(RC) sc O(RC) “”” hasCycle = False seen = set() r = len(grid) c = len(grid[0]) def dfs(i,j,pre,seen,char): res = False seen.add((i,j)) for x,y in [(i+1,j),(i-1,j),(i,j+1),(i,j-1)]: if 0<=x<r and 0<=y<c: if x != pre[0] or y != pre[1]: if grid[x][y]... [Read More]

LC1552. Magnetic Force Between Two Balls

class Solution: def maxDistance(self, A: List[int], m: int) -> int: """ tc O(NlgM) sc O(1), M: diff of max(pos) - min(pos) binary search: range [min-min, max-min], note ball has to be placed in position exists in A intv: minimun distance between any two balls is intv main idea: We want... [Read More]

LC1544. Make The String Great

```python class Solution: def makeGood(self, s: str) -> str: “”” main idea: stack, keep tracking current char and stack top if they are lower and upper case tc O(N) sc O(N) “”” st = [] n = len(s) for i in range(n): if st and abs(ord(s[i])-ord(st[-1])) == 32: st.pop() continue... [Read More]
Tags: String Stack