Problems
August 16, 2022
String to integer atoi
First we will strip any whitespace characters, and then check if the length is zero, we immediately return 0. Then we check the sign, if the sign is +
or -
, we keep that in...
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...
ReadAugust 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...
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...
ReadAugust 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,...
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...
ReadAugust 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...
ReadAugust 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,...
ReadAugust 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...
ReadAugust 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