Upgrade checkAnagram function & Fixes#902
Upgrade checkAnagram function & Fixes#902raklaptudirm merged 14 commits intoTheAlgorithms:masterfrom fahimfaisaal:upgrade-anagram
checkAnagram function & Fixes#902Conversation
Looks like the algorithm is less efficient now. It was O(n) and now is O(n^2) |
|
@kikar I think the new implementation can teach more Javascript concepts than the previous one, which is our primary aim. That said, I think it will be better to have the two implementations separately @fahimfaisaal. |
|
@kikar to my knowledge, the So, the algorithm couldn't be O(n^2) complexity. |
Even if use regex, the code needs to find the first occurrence of the letter selected. Take the case of the reversed string. |
Yes, It's definitely not O(1) but ES6 doesn't run the search algorithm:
but in the long string, it will be slower. |
@raklaptudirm I think if I optimize my algorithm, It's shouldn't add two more implementations. please let me know what could I do. |
|
Since the two implementations are very different, I would suggest to keep and test both. |
|
@raklaptudirm I reckon, if we add this algorithm it's will replace previous algos. const str1List = Array.from(str1.toUpperCase()) // str1 to array
// get the occurrences of characters of str1 by using HashMap
const str1Occurs = str1List.reduce(
(map, char) => (
map.set(char, map.get(char) + 1 || 1), map
),
new Map()
)
for (const char of str2.toUpperCase()) {
// if char has not exist to the map it's return false
if (!str1Occurs.has(char)) {
return false;
}
let getCharCount = str1Occurs.get(char)
str1Occurs.set(char, --getCharCount)
getCharCount === 0 && str1Occurs.delete(char)
}
return true;
} |
|
Both do not need to have the same name. |
|
Ok I will add these two with different names, but could I add these two algos in One file? |
|
Yes. |
|
Ok, thanks for your rapid response. |
raklaptudirm
left a comment
There was a problem hiding this comment.
Rename the functions to remove via from the name.
|
You're really strict reviewer. 😀 |
checkAnagram function & Fixes
Welcome to the JavaScript community
Describe your change:
Array.prototype.reduce&String.prototype.replacemethodsRegExpChecklist:
Fixes: #{$ISSUE_NO}.