Example 1: Input: s = "applepenapple", wordDict = Output: true Explanation: "applepenapple" can be seperated as "apple pen apple". This problem can be solved by using DP . We shall have a look at the code, before explaining the code. Solution in C++ #include<iostream> …
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 …