NeetCode 1 word solutions Flashcards
W: Find Duplicate Elements in Array
Duplicates cannot be added in Hashmap.
Find if two strings are anagram of each other
2 Hashmaps.
Add character as key and count as value.
Compare 2 Hashmaps.
TWO SUM in an array
1 Hashmap.
Calculate difference = Target - n.
If Difference present in Map return. Else add it to hashmap.
GROUP ANAGRAMS
o(m*n)
hashmap(list)
key = [list of same char/value count]
value = [list of strings]
TOP K Frequent Elements
O(m+n)
Reverse bucket sort.
First count the nums using hashmap.
Array - index is count , value is array of numbers with that (index) count.
Read array from last index
Encode and Decode String
1) Add number of characters followed by # before every string
2) Seperate each string with ; , If it already has ; , add \ esacpe character before every special character. to tell that consider the next character after \ as a part of string
3) JSON Serialisation 4) Hexadecimal;
Product of Array except self without /
1) Using / is easy with O(n)
2) Prefix array * Postfix array - takes up space.
3) Single output array. Prefix variable and post fix variable.
Key is to fill the prefix/postfix variable value in the array position.
Longest Sequence in An Array
Use the SET.
FInd if the value has previous element. If no then it is start of a sequence. Then find the next elements present or not.