Skip to main content

Posts

Showing posts from August, 2022

Leetcode 212. Word Search II. Python (TrieNode/Prefix Tree)

  212 .  Word Search II Given an  m x n   board  of characters and a list of strings  words , return  all words on the board . Each word must be constructed from letters of sequentially adjacent cells, where  adjacent cells  are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word. Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"] Output: ["eat","oath"] Constraints: m == board.length n == board[i].length 1 <= m, n <= 12 board[i][j]  is a lowercase English letter. 1 <= words.length <= 3 * 10 4 1 <= words[i].length <= 10 words[i]  consists of lowercase English letters. All the strings of  words  are unique. class TrieNode: de

Leetcode 217. Contains Duplicate. Python (Easiest Approach ✅)

217 .  Contains Duplicate   Given an integer array  nums , return  true  if any value appears  at least twice  in the array, and return  false  if every element is distinct.   Example 1: Input: nums = [1,2,3,1] Output: true Example 2: Input: nums = [1,2,3,4] Output: false Example 3: Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true   Constraints: 1 <= nums.length <= 10 5 -10 9  <= nums[i] <= 10 9 class Solution: def containsDuplicate(self, nums: List[int]) -> bool: hs = set() for n in nums: if n in hs: return True hs.add(n) return False Explaination :

Leetcode 213. House Robber II. Python (Easy)

213 .  House Robber II   You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are  arranged in a circle.  That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and  it will automatically contact the police if two adjacent houses were broken into on the same night . Given an integer array  nums  representing the amount of money of each house, return  the maximum amount of money you can rob tonight  without alerting the police .   Example 1: Input: nums = [2,3,2] Output: 3 Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses. Example 2: Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. Example 3: Input: nums = [1,2,3] Output: 3   Constraints: 1 <= nums.length <= 100

Leetcode 211. Design Add and Search Words Data Structure. Python (Prefix Tree)

  211 .  Design Add and Search Words Data Structure Design a data structure that supports adding new words and finding if a string matches any previously added string. Implement the  WordDictionary  class: WordDictionary()  Initializes the object. void addWord(word)  Adds  word  to the data structure, it can be matched later. bool search(word)  Returns  true  if there is any string in the data structure that matches  word  or  false  otherwise.  word  may contain dots  '.'  where dots can be matched with any letter.   Example: Input ["WordDictionary","addWord","addWord","addWord","search","search","search","search"] [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]] Output [null,null,null,null,false,true,true,true] Explanation WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("bad"

Leetcode 208. Implement Trie. Python (Prefix Tree)

208 .  Implement Trie (Prefix Tree) A  trie  (pronounced as "try") or  prefix tree  is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker. Implement the Trie class: Trie()  Initializes the trie object. void insert(String word)  Inserts the string  word  into the trie. boolean search(String word)  Returns  true  if the string  word  is in the trie (i.e., was inserted before), and  false  otherwise. boolean startsWith(String prefix)  Returns  true  if there is a previously inserted string  word  that has the prefix  prefix , and  false  otherwise.   Example 1: Input ["Trie", "insert", "search", "search", "startsWith", "insert", "search"] [[], ["apple"], ["apple"], ["app"], ["app"], ["app"], ["app"]] Output [null, null

Leetcode 207. Course Schedule. Python

207 .  Course Schedule There are a total of  numCourses  courses you have to take, labeled from  0  to  numCourses - 1 . You are given an array  prerequisites  where  prerequisites[i] = [a i , b i ]  indicates that you  must  take course  b i  first if you want to take course  a i . For example, the pair  [0, 1] , indicates that to take course  0  you have to first take course  1 . Return  true  if you can finish all courses. Otherwise, return  false .   Example 1: Input: numCourses = 2, prerequisites = [[1,0]] Output: true Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible. Example 2: Input: numCourses = 2, prerequisites = [[1,0],[0,1]] Output: false Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.   Constraints: 1 <= numCourses <= 2000 0 <= prerequisites.length

Leetcode 206. Reverse Linked List. Python (The Best Approch)

  206 .  Reverse Linked List Given the  head  of a singly linked list, reverse the list, and return  the reversed list .   Example 1: Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: Input: head = [1,2] Output: [2,1] Example 3: Input: head = [] Output: []   Constraints: The number of nodes in the list is the range  [0, 5000] . -5000 <= Node.val <= 5000 class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: p, c = None, head while c: temp = c.next c.next = p p = c c = temp return p Explaination :

Leetcode 200. Number of Islands. Python

Given an  m x n  2D binary grid  grid  which represents a map of  '1' s (land) and  '0' s (water), return  the number of islands . An  island  is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.   Example 1: Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] Output: 1 Example 2: Input: grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] Outpu

Leetcode 198. House Robber. DP. Python

  198 .  House Robber You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and  it will automatically contact the police if two adjacent houses were broken into on the same night . Given an integer array  nums  representing the amount of money of each house, return  the maximum amount of money you can rob tonight  without alerting the police .   Example 1: Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. Example 2: Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.   Constraints: 1 <= nums.length <= 100 0 <= nums[i] <= 400 class Solution: def rob(self, nums: List[int]) -> i