Example 1: Input: Output: 3 We can solve this by 2 methods: unordered_map XOR operation. The solution for “unordered_map” is same as the solution for “single-number”. Below explains in details about the solution using BIT Manipulation. If the problem were this: “one element appears once, all …
There are N gas stations along a circular route, where the amount of gas at station i is gas. You have a motorcycle with an unlimited gas tank and it costs cost of gas to travel from station i to its next station (i+1). You begin …
Example: Input: "aab" Output: , ] The solution for this problem can be done with the help of DFS. The logic is to loop through the string one by one. Once we find a palindromic string for the string str(0, i) we store the current string …
Example 1: Input: Output: 1 We can solve this by 2 methods: unordered_map XOR operation. We shall discuss both of the solutions in this tutorial. Solution using unordered_map. Use unordered_map, increment the “second” in unordered_map when ever you get an element. At the end, iterate throughout …
Given a begin_word and end_word, find the words that transforms from begin_word to end_word. Example 1: Input: beginWord = "hit", endWord = "cog", wordList = Output: 5 Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog", return its length 5. …
Example 1: Input: Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Not 7-1 = 6, as selling price needs to be larger than buying price. The solution for this problem is …
For example, given the following triangle , , , ] The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). Follow up question: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle. …
Below are the rules for a Sudoku to be valid: Each row must contain the digits 1-9 without repetition. Each column must contain the digits 1-9 without repetition. Each of the 9 3×3 sub-boxes of the grid must contain the digits 1-9 without repetition. Example 1: Input: , , , , , …