Problems
November 29, 2022
Reverse words in a string II
We will reverse the entire string, then reverse each word. We will use two pointers to keep track of the start and end of each word.
Time complexity: O(n)
, n is the length of the...
November 29, 2022
Generate a string with characters that have odd counts
If n is odd, we will return a string with n 'a's. If n is even, we will return a string with n-1 'a's and one 'b'.
Time complexity: O(n)
, n is the number of characters in the string...
November 29, 2022
Count univalue subtrees
We will use postorder traversal to count the number of univalue subtrees. We will also use a helper function to check if the current node is a univalue subtree.
Time complexity: O(n)
,...
November 29, 2022
Longest subarray of 1s after deleting one element
We will use two pointers to keep track of the start and end of the subarray. We will use a variable to keep track of the number of zeros in the subarray. If the number of zeros is less than or equal...
ReadNovember 29, 2022
Print immutable linked list in reverse
We can use a stack to store the values of the linked list. We will then pop the values from the stack and print them.
Time complexity: O(n)
, n is the number of nodes in the linked...
November 29, 2022
Inorder successor in BST
As we are traversing a binary search tree, we can use it's property to find the inorder successor. The inorder successor of a node is the leftmost node in the right subtree. If the node does not have...
ReadNovember 28, 2022
Binary tree vertical order traversal
We will use BFS to traverse the tree. We will keep track of the horizontal distance of each node from the root. We will use a dictionary to store the nodes at each horizontal distance. We will use a...
ReadNovember 28, 2022
Minimize product sum of two arrays
We will sort the two arrays, first one in ascending order and second one in descending order. That will make sure the product of both array's item will be minimized. Then we will iterate over the...
ReadNovember 28, 2022
Validate binary tree nodes
We will first find the root of the tree. Then we will traverse the tree and check if we can reach all the nodes from the root. We will use BFS to traverse. If we can reach all the nodes, then the...
ReadNovember 28, 2022
Remove zero sum consecutive nodes from linked list
We will keep track of the prefix sum of the linked list. If the prefix sum is repeated, we will remove the nodes between the repeated prefix sum and the current prefix sum. Time complexity:...
Read