LC290 Word Pattern

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]
Tags: Hash Table

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]
Tags: Hash Table

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]