Problems


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

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

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

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

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

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

Count pairs in two arrays

nums1[i] + nums1[j] > nums2[i] + nums2[j], i < j can be converted to (nums1[i]-nums2[i]) + (nums1[j]-nums2[j]) > 0, i < j. At this point, we will realize that...

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