Problems


August 22, 2022

Check if a word occurs as a prefix of any word in a sentence

We will split the string with space then iterate over each word to check the prefix, if we find one, we return the position of the word in the sentence, else we return -1. Time Complexity:...

Read

August 22, 2022

Power of four

We will devide the number by 4 until it's 1. If in any step, we have any reminder, we will return false, otherwise return true. Time Complexity: O(log(n)) Space Complexity:...

Read

August 22, 2022

Minimum deletions to make string balanced

We can count the number of occurance of b and then reduce the count with an upcoming a character, also increase the result count on the way. Finally return our result count...

Read

August 21, 2022

Construct binary tree from preorder and postorder traversal

We know, the first value of preorder traversal is always the root. From than we can determine the the left subtree's preorder and postorder traversal, and right subtree's preorder and postorder...

Read

August 21, 2022

Check if the sentence is pangram

We will count each character and add it to a hashmap. If the number of characters are equal to 26, we return true otherwise return false. Time Complexity: O(n) Space Complexity:...

Read

August 21, 2022

Number of good ways to split a string

We will first count the characters in the string. Then we iterate over the string, add this to the set, and remove the occurance from the count. Then if the length of the set is equal to the length...

Read

August 21, 2022

Cousins in binary tree

We will run a BFS in the tree, and will keep track of the parents, if we find a parent, in a level for one node, we will return false, if we find two parent in a row, we return true. If we find the...

Read

August 21, 2022

Stamping the sequence

We will start from the target, then move backwords towards our source, which is a string of ?. We will take a sliding window of length M, which is the length of the stamp, check whether...

Read

August 20, 2022

Number of pairs of strings with concatenation equal to target

First we will count the number of frequency of each elements of the input. Then iterate over all possible slices of target and multiply by the count and add it to our final result. If some slices are...

Read

August 20, 2022

The k weakest rows in a matrix

First we iterate through the whole matrix to count the number of 1's in the matrix and put it in an min heap. Then we extract top k elements from that heap. Time Complexity:...

Read
... 72 73 74 75 76 ...