This is the original problem :
Input Format
The first line contains a single string, a. The second line contains a single string, b.
Constraints
1<= |a|,|b| <= 10^4
It is guaranteed that and consist of lowercase English alphabetic letters (i.e., through ). Output Format
Print a single integer denoting the number of characters you must delete to make the two strings anagrams of each other.
Sample Input
cde
abc
Sample Output
4
Explanation
We delete the following characters from our two strings to turn them into anagrams of each other:
Remove d and e from cde to get c. Remove a and b from abc to get c. We must delete characters to make both strings anagrams, so we print on a new line.
And this is the solution I've came up with using javascript. I've decided to use objects in order to avoid nested for loops which leads to O(M*N). I think my solution is O(M+N+O+P), however, I do believe there's a much better solution out there, and some more refactoring can be done to my code. Anyone?
There are some default I/O codes you may find in the original website
function main() { var a = readLine(); var b = readLine(); // Creating object with {"k": 5, "a": 2, "b": 1} for example var objA = countAlphabetFrequency(a); var objB = countAlphabetFrequency(b); var numOfDeletionsA = countNumberOfDeletions(objA,objB); var numOfDeletionsB = countNumberOfDeletions(objB,objA); console.log(numOfDeletionsA + numOfDeletionsB); } function countAlphabetFrequency (arrOfAlphabets){ var resultObj = {} for (i = 0; i < arrOfAlphabets.length; i++) { if (resultObj[arrOfAlphabets[i]]) { resultObj[arrOfAlphabets[i]] += 1; } else { resultObj[arrOfAlphabets[i]] = 1; } } return resultObj; } function countNumberOfDeletions (mainObj, referenceObj){ var numOfDeletions = 0; for (var k in mainObj) { if (mainObj.hasOwnProperty(k)) { if (mainObj[k] && referenceObj[k]) { // Alphabet k exists in both strings if (mainObj[k] > referenceObj[k]) { // Main string has more k than in reference string numOfDeletions += mainObj[k] - referenceObj[k]; mainObj[k] = referenceObj[k]; } } else { // Alphabet k only exists in Main string numOfDeletions += mainObj[k]; } } } return numOfDeletions }