class Solution: def wordPattern(self, pattern: str, str: str) -> bool: """ time O(N) space O(2M) M: unique # words step1: split str, check length of str and pattern step2: create two hash map, loop each element in str/ pattern step3: check if pattern[i] in d1 / \ yes if str[i]...
[Read More]
LC252 Meeting Rooms
brote force time O(N^2) space O(N)
[Read More]
LC409 Longest Palindrome
# time O(N) space O(K) since alphabetic but technically O(lgN) import collections class Solution: def longestPalindrome(self, s: str) -> int: d = collections.Counter(s) cnt = 0 odd = False for c in d.keys(): if d[c] >= 2: cnt += d[c]//2 * 2 if d[c] % 2 == 1: odd =...
[Read More]
LC34 Find First and Last Position of Element in Sorted Array
class Solution: # test case : 5 7 7 8 8 10, t = 8 # [1], 1 => [0,0] # [1], 2 =>[-1,-1] # 1 2 2 => def searchRange(self, nums: List[int], target: int) -> List[int]: # edge case: len(nums) == 1 or 0 # brute force: loop O(N)...
[Read More]
LC242 Valid Anagram
class Solution: def isAnagram(self, s: str, t: str) -> bool: # main idea: create fixed array to record amount of time each char in s has appeared # time O(N) space O(K) , K = 26 if len(s) != len(t): # optimization1: to exit early return False l = [0]*26...
[Read More]