-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Upgrade checkAnagram function & Fixes
#902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade checkAnagram function & Fixes
#902
Conversation
Optimize the algorithm via Array.prototype.reduce & String.prototype.replace methods
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 String.prototype.replace method used regex that's why the operation is very cheap, cause it's not traversing the full string str2Acc in every traverse of Array.prototype.reduce method. it just removes 1 char from str2Acc in 1 Array.prototype.reduce method traverse. which char is just matched to the cur variable.
So, the algorithm couldn't be O(n^2) complexity.
kikar
commented
Feb 24, 2022
@kikar to my knowledge, the
String.prototype.replacemethod used regex that's why the operation is very cheap, cause it's not traversing the full stringstr2Accin every traverse ofArray.prototype.reducemethod. it just removes 1 char fromstr2Accin 1Array.prototype.reducemethod traverse. which char is just matched to thecurvariable.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.
For each letter from str1 you need to traverse the whole str2 to find the letter to remove.
@kikar to my knowledge, the
String.prototype.replacemethod used regex that's why the operation is very cheap, cause it's not traversing the full stringstr2Accin every traverse ofArray.prototype.reducemethod. it just removes 1 char fromstr2Accin 1Array.prototype.reducemethod traverse. which char is just matched to thecurvariable.
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. For each letter from str1 you need to traverse the whole str2 to find the letter to remove.
Yes, It's definitely not O(1) but ES6 doesn't run the search algorithm:
Search string for the first occurrence of searchString and let pos be the index within a string of the first code unit of the matched substring and let matched be searchString. If no occurrences of searchString were found, return string.
but in the long string, it will be slower.
@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.
@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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename the functions to remove via from the name.
You're really strict reviewer. 😀
checkAnagram function & Fixes (追記ここまで)
Welcome to the JavaScript community
Open in Gitpod know more
Describe your change:
Array.prototype.reduce&String.prototype.replacemethodsRegExpChecklist:
Fixes: #{$ISSUE_NO}.