Problems


August 16, 2022

N-th tribonacci number

This problem is just like fibonacci number, we will first solve it recursively and then memoize it to reduce redundant calculation. Time Complexity: O(n) Space Complexity:...

Read

July 21, 2022

Prime number of set bits in binary representation

This is a very strait forward problem. We will have 2 helper function, one is for counting the number of bits in an integer and another for checking if the number is prime or not. Then we iterate...

Read

August 15, 2022

Roman to integer

We can replace characters like IV or IX to IIII and VIIII. In that way, when we calculate our result, we can just translate the value to integer...

Read

August 14, 2022

Next greater element I

We traverse nums2 and start storing elements on the top of stack. If current number is greater than the top of the stack, then we found a pair. Keep popping from stack till the top of stack is...

Read

August 14, 2022

Word ladder II

We will first run a BFS to create the graph with adjacency list. Then we will run DFS to find all possible paths of that graph. Time Complexity: O((n+m)*l), n is the number of words,...

Read

August 13, 2022

Distinct subsequences

We will check each character of both string, when we find a match, then we have 2 choice, either we include the current index or we skip it. If we don't have a match, we we will skip the current...

Read

August 13, 2022

Edit distance

To compute the minimum distance, we will recursively call our decision tree. If i is 0, then we need to insert j characters, if j = 0, then we need in delete i characters. This will be our base case,...

Read

August 13, 2022

Design circular queue

We can implement a circular queue by using an array, the maximum size of the array will be the given input k. Then we will keep 2 pointers, one for head and one for tail, and whenever we enqueue some...

Read

August 13, 2022

Regular expression matching

We will go through our decision tree, and then we have 2 choices, either we take the * or we skip it. Based on this logic, we will create a recursive DFS solution for our problem. Then we memoize it...

Read

August 13, 2022

Stream of characters

We can use a trie to search efficiently in the stream. When we initially create our StreamChecker class, we will insert every word in reverse word. Then whenever we search for a character, we add...

Read
... 74 75 76 77 78 ...