File tree Expand file tree Collapse file tree 5 files changed +104
-0
lines changed
best-time-to-buy-and-sell-stock
design-add-and-search-words-data-structure
encode-and-decode-strings Expand file tree Collapse file tree 5 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ # 121. Best Time to Buy and Sell Stock
3+ # O(n) - Two Pointers
4+ class Solution :
5+ def maxProfit (self , prices : list [int ]) -> int :
6+ left = 0
7+ right = 1
8+ profit = 0
9+ max_profit = 0
10+
11+ while right < len (prices ):
12+ if prices [right ] > prices [left ]:
13+ profit = prices [right ] - prices [left ]
14+ max_profit = max (profit , max_profit )
15+ else :
16+ left = right
17+ right += 1
18+ return max_profit
Original file line number Diff line number Diff line change 1+
2+ # 208. Implement Trie (Prefix Tree)
3+ class Trie :
4+
5+ def __init__ (self ):
6+ self .list = []
7+
8+
9+ def insert (self , word : str ) -> None :
10+ self .list .append (word )
11+
12+
13+ def search (self , word : str ) -> bool :
14+ if word in self .list :
15+ return True
16+ else :
17+ return False
18+
19+
20+ def startsWith (self , prefix : str ) -> bool :
21+ for i , value in enumerate (self .list ):
22+ if value .startswith (prefix ):
23+ return True
24+ return False
25+
26+
27+
28+ # Your Trie object will be instantiated and called as such:
29+ # obj = Trie()
30+ # obj.insert(word)
31+ # param_2 = obj.search(word)
32+ # param_3 = obj.startsWith(prefix)
Original file line number Diff line number Diff line change 1+
2+
3+ # 271. Encode and Decode Strings
4+
5+ # integer represents the following word's length
6+ # read until we reach to delimiter
7+
8+ class Solution :
9+ def encode (self , strs ):
10+ res = ""
11+ for s in strs :
12+ res += str (len (s )) + "#" + s
13+ return res
14+
15+ def decode (self , str ):
16+ res , i = [], 0
17+
18+ while i < len (str ):
19+ j = i
20+ while str [j ] != "#" :
21+ j += 1
22+ length = int (str [i :j ])
23+ res .append (str [j + 1 : j + 1 + length ])
24+ i = j + 1 + length
25+
26+ return res
Original file line number Diff line number Diff line change 1+ from collections import defaultdict
2+ class Solution :
3+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
4+ # hashmap
5+ # calculate frequency
6+ # 26 alphabet characters
7+ anagrams_dict = defaultdict (list )
8+ for s in strs :
9+ count = [0 ] * 26
10+ for c in s :
11+ count [ord (c ) - ord ('a' )] += 1
12+
13+ key = tuple (count )
14+ anagrams_dict [key ].append (s )
15+
16+ return list (anagrams_dict .values ())
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def wordBreak (self , s : str , wordDict : List [str ]) -> bool :
3+ dp = [True ] + [False ] * len (s )
4+
5+ for i in range (1 , len (s ) + 1 ):
6+ for w in wordDict :
7+ start = i - len (w )
8+ if start >= 0 and dp [start ] and s [start :i ] == w :
9+ dp [i ] = True
10+ break
11+
12+ return dp [- 1 ]
You can’t perform that action at this time.
0 commit comments