Problems
December 25, 2022
Take k of each character from left and right
We will count the occurance of a, b and c. Then we will iterate over the string and take k characters from the left and right. We will update the result.
Time complexity: O(n)
Space...
December 25, 2022
Minimum number of moves to make palindrome
We will iterate over the string and count the number of moves for each character. We will update the result.
Time complexity: O(n^2)
Space complexity: O(1)
December 25, 2022
Count anagrams
It is obvious that the answer is the product of the number of unique permutations for each word in a sentence. The last one is just the number of permutations of all letters (treating same letters as...
ReadDecember 25, 2022
Longest subsequence with limited sum
We will sort the numbers and then create a prefix sum array. Then we will iterate over the numbers and find the index of the largest number that is less than or equal to limit - nums[i]
....
December 24, 2022
Beautiful array
We will use recursion to solve the problem. We will call the helper function on odd numbers and even numbers. We will return the result.
Time complexity: O(n)
Space complexity:...
December 24, 2022
Custom sort string
We will use a hashmap to store the order of characters in order
. We will iterate over s
and store the characters in a list. We will sort the list using the order of...
December 24, 2022
Valid triangle number
We will sort the array. We will iterate over the array and use two pointers to find the number of valid triangles. We will return the result.
Time complexity: O(n^2)
Space...
December 24, 2022
Number of distinct islands
We will use a set to store the islands. We will iterate over the grid and call the dfs function on the current cell. We will return the result.
Time complexity: O(mn)
Space...
December 24, 2022
Domino and tromino tiling
We will use dynamic programming to solve the problem. We will use dp[i]
to store the number of ways to tile i
units. We will use dp[i] = dp[i-1] + dp[i-2] + 2 *...
December 23, 2022
Convert binary search tree to sorted doubly linked list
We do an inorder traversal and make an arr of sorted nodes. We then create the doubly linked list by iterating through the array.
Time complexity: O(n)
Space complexity:...