Problems


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...

Read

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...

Read

November 29, 2022

Valid palindrome III

We will solve the problem using top-down dynamic programming. If the first and last characters are the same, we will check if the substring between the first and last characters is a valid...

Read

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...

Read

November 29, 2022

Count of smaller numbers after self

We will make a sorted copy of the nums. Then we iterate over each number, find the index of the number in the sorted copy, and add the number of numbers smaller than the number to the result. Time...

Read

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...

Read

November 28, 2022

Count number of bad pairs

Rather counting the number of bad pair, we can count the number of good pair and substruct it from to total number of pairs to get the number of bad pairs. For an array of size n, there are exactly n...

Read

November 28, 2022

Design phone directory

We will use a set to keep track of the available numbers. We will also use a queue to keep track of the numbers that have been released. Time complexity: O(1) Space complexity:...

Read

November 28, 2022

Shortest word distance

We will keep track of both words' last seen index. Then we will iterate through the words array and update the result if we find either of the words. Finally, we will return the result....

Read

November 28, 2022

Count nodes equal to sum of descendants

We will use DFS in post order traversal to solve the problem. We will recursively call the function on the left and right child of the root. Then we will check if the sum of the left and right child...

Read
... 27 28 29 30 31 ...