Topic Problem
Arrays Contains Duplicate
String Valid Anagram
Arrays Two Sum
Arrays, String Group Anagrams
Arrays Top K Frequent Elements
String Encode and Decode Strings
Arrays Products of Array Except Self
Arrays Valid Soduko
String Valid Palindrome
Arrays Two Sum Integer II
Arrays 3Sum
Arrays Longest Consecutive Sequence
Arrays Container with Most Water
Arrays Trapping Rain Water
Arrays Best Time to Buy and Sell Stocks
Longest Substring Without Repeating
String
Characters
String Longest Repeating Character Replacement
String Permutation in String
Initial Approach
My approach was to use a HashMap and store the frequencies of each element and if the
frequency is greater than 1, return true otherwise false. O(n) time and space complexity.
First checked if the lengths are equal, then created an array of size 26 and incremented
the position (character - 'a') for the first string and then decremented the position for the
second string. If all the values are 0 then it is an anagram or else it is not.
Create a HashMap storing the (Value, Index). Then iterate once again but checking if
target-currentValue exists in the HashMap and both the indices are not the same, then
return the indicies.
Create a HashMap and store (String key, ArrayList<String>). The string key is going to be
the array which contains the incremented values of the indexes of the characters in the
particular string. Make sure to only put the key in the HashMap if it is absent. Keep adding
the strings with the same key. SImilar to Valid Anagram. O(m * n) as we have to iterate
through the array as well as each characters.
Using a min heap which stores (Frequency, Number) and the sorted using the number as
the comparator. (a, b) -> a[0] - b[0] this sorts in ascending order based no frequency. Then
keep polling only only k elements are left in the heap and add them to the output array.
Using normal substring and split operation. Wouldn’t work when we have a ',' in the string
element.
Find the product of all numbers except 0 and then divide the particular number with the
product if it is not 0. if there is a 0, then the product except 0 is only displayed at the
indices where there is 0.
Loop through each row and each column to check for repeated elements using HashSet.
For verifying each 3x3 matrix we should divide the big board to 3x3 matrix with indices 0,1,
and 2. After that we need to add the element at row (index / 3) * 3 + i and at column
(index % 3) * 3 + j to the HashSet and then check if there are duplicates.
Using StringBuilder to add only characters that are letter or digit and making sure we only
append after converting the character to lower case as we are ignoring case. Then return
whether the StringBuilder string is equal to its reverse.
Using two pointers, one in the begenning and other in the end, to check the indices that
match the target.
Loop over all the elements and then use the two pointer method to get the 2nd and 3rd
element that matches the condition.
Add all the elements to a set. Then check if the element has a previous element i.e.
element - 1 is present in the set or not. If it is not present then it is the start of a series.
Once the start has been identified check how long it is by adding 1 and checking if it is
present and then store the max of previous length and current length.
Create 2 pointers, left and right. Then while left is less than right. Area of particular
segment is minimum of left and right element multiplied by the difference or the elements
between left and right. (area = min(left, right) * (left - right)). Then keep incrementing left
is the element is lower than right and decrement right if it is lower than left. Store the max
area in a variable.
Make 2 arrays that stores the Maximum element to the left of that particular index and
another that stores the Maxium to the right of that particular index. Area at a particular
index is the Min(leftMax, rightMax) - element at that index. If the value of this calculation is
greater than 0, then add it to the final answer.
Using 2 pointers. First pointer at the begenning and the secong pointer at the 2nd element
and we move the 2nd pointer if it is in a profit and if it is in a loss we move the first one.
Using the Slinding Window technique. We create a left pointer and a for loop for the right
pointer. If the current character at the right is present in the character set, remove the left
element from set until the current element is removed and then add it to the set and save
the max length in a variable. The max length would be the max of previous max and the
(right - left + 1).
Create a HashSet and all the characters that are there in the string. Then iterate over each
character. Create variables count and left pointer. Using a for loop for the right [Link]
the right pointer character is equal to the current character then count++. Create a nested
while loop and while (right - left + 1) - count > k, if the left pointer character is equal to the
current character, count-- and left++ i.e. shrinking the sliding window. the max length is
the max of previous max length and (right - left + 1).
Optimized Approach
[Link](nums).distinct().count() < [Link] OR create a hashset and add all the elements
but also add a condition whether the current number is present, if yes then return true else contin
My approach OR create 2 HashMaps with frequencies and then use the .equals() method to compa
HashMaps.
Create a HashMap storing the (Value, Index). Then while adding the elements, check if the target-cu
present in the HashMap so far, if it is then return the indicies or continue with adding the element.
checks all the elements added before the current value.
Instead of using int array, we can use char array and then sort it and make it a string and use that a
optimized due to sorting. O(m * nlogn) time complexity.
Bucket Sort. Create an array with its index position being the frequency of the times a particular num
in each index position as multiple numbers can have the same frequency) and keep adding to the
particular index the numbers with that frequency. Return the top most frequently appearing elements
the index the more frequently it appears.
Better to use the length and a special character (eg - #) to differentiate each element. The length wil
pointers and so on. Stringbuilder to encode and index play for the decode.
It is better to calculate prefix and postfix and then multiply them together. First iteration to calculat
next iteration to multpile the prefix array with the postfix. Then return the array.
TODO
Have two pointers, one for the left and one for the right and use a while loop till they don't overlap
Then increment left and right if it is not a valid character. Then check if the next valid character is th
then it is not a palindrom.
Can use HashMap but two pointer most optimal.
Can use HashMap but two pointer most optimal.
TODO
2 pointers
2 pointers. Left and right. 2 variables LeftMax and RightMax. If the LeftMax is smaller than the RightM
left pointer, recalculate the LeftMax, add to the final area the difference between LeftMax and Value
Same applies for right pointer and RightMax.
Dynamic Programming TODO
Also using Sliding Window. Using a HashMap. We have a left pointer as well as a for loop for the righ
store the character and the right index in the HashMap. If the hashmap has the character at the righ
gets updated to the ++right. Max length is the max of previous max and the (right - left +
Using a HashMap. We store the character and the count of that particular character. We create left p
max frequency variable. Update the max frequency each time the with the max of previous max freq
frequency of the right pointer character. Create a nested while loop to update the left pointer and d
frequency of the left pointer character while (right - left + 1) - max frequency > k. Updated the max l
with the max of previous max and (right - left + 1).