├── README.md ├── concat.py ├── largest.py └── autocomplete.py /README.md: -------------------------------------------------------------------------------- 1 | # Three-Coding-Problems 2 | 3 | Video explanation at https://www.youtube.com/watch?v=QGVCnjXmrNg 4 | 5 | Get 100+ more coding videos at http://coderpro.com/ 6 | -------------------------------------------------------------------------------- /concat.py: -------------------------------------------------------------------------------- 1 | # Concatenate Words 2 | # Video explanation at https://www.youtube.com/watch?v=QGVCnjXmrNg 3 | # Get 100+ more coding videos at http://coderpro.com/ 4 | 5 | class Solution(object): 6 | def findAllConcatenatedWords(self, words): 7 | wordDict = set(words) 8 | cache = {} 9 | return [word for word in words if self._canForm(word, wordDict, cache)] 10 | 11 | def _canForm(self, word, wordDict, cache): 12 | if word in cache: 13 | return cache[word] 14 | for index in range(1, len(word)): 15 | prefix = word[:index] 16 | suffix = word[index:] 17 | if prefix in wordDict: 18 | if suffix in wordDict or self._canForm(suffix, wordDict, cache): 19 | cache[word] = True 20 | return True 21 | cache[word] = False 22 | return False 23 | 24 | input = ['cat', 'cats', 'dog', 'catsdog'] 25 | print(Solution().findAllConcatenatedWords(input)) 26 | # ['catsdog'] 27 | -------------------------------------------------------------------------------- /largest.py: -------------------------------------------------------------------------------- 1 | # K-th Largest Number 2 | # Video explanation at https://www.youtube.com/watch?v=QGVCnjXmrNg 3 | # Get 100+ more coding videos at http://coderpro.com/ 4 | 5 | class Solution(object): 6 | def findKthLargest(self, arr, k): 7 | left = 0 8 | right = len(arr) - 1 9 | while left <= right: 10 | pivotIndex = self._partition(arr, left, right) 11 | if pivotIndex == len(arr) - k: 12 | return arr[pivotIndex] 13 | elif pivotIndex > len(arr) - k: 14 | right = pivotIndex - 1 15 | else: 16 | left = pivotIndex + 1 17 | return -1 18 | 19 | def _partition(self, arr, low, high): 20 | pivot = arr[high] 21 | index = low 22 | for j in range(low, high): 23 | if arr[j] <= pivot: 24 | arr[index], arr[j] = arr[j], arr[index] 25 | index += 1 26 | arr[index], arr[high] = arr[high], arr[index] 27 | return index 28 | 29 | 30 | print(Solution().findKthLargest([5, 7, 2, 3, 4, 1, 6], 3)) 31 | # 5 32 | -------------------------------------------------------------------------------- /autocomplete.py: -------------------------------------------------------------------------------- 1 | # Autocompletion 2 | # Video explanation at https://www.youtube.com/watch?v=QGVCnjXmrNg 3 | # Get 100+ more coding videos at http://coderpro.com/ 4 | 5 | class Node: 6 | def __init__(self, children, isWord): 7 | self.children = children 8 | self.isWord = isWord 9 | 10 | class Solution: 11 | def __init__(self): 12 | self.trie = None 13 | 14 | def build(self, words): 15 | self.trie = Node({}, False) 16 | for word in words: 17 | current = self.trie 18 | for char in word: 19 | if not char in current.children: 20 | current.children[char] = Node({}, False) 21 | current = current.children[char] 22 | current.isWord = True 23 | 24 | def autocomplete(self, prefix): 25 | current = self.trie 26 | for char in prefix: 27 | if not char in current.children: 28 | return [] 29 | current = current.children[char] 30 | return self._findWordsFromNode(current, prefix) 31 | 32 | def _findWordsFromNode(self, node, prefix): 33 | words = [] 34 | if node.isWord: 35 | words += [prefix] 36 | for char in node.children: 37 | words += self._findWordsFromNode(node.children[char], prefix + char) 38 | return words 39 | 40 | s = Solution() 41 | s.build(['dog', 'dark', 'cat', 'door', 'dodge']) 42 | print(s.autocomplete('do')) 43 | # ['dog', 'door', 'dodge'] 44 | --------------------------------------------------------------------------------