3
\$\begingroup\$

Here is the original problem, and here's my solution:

function main() {
 var m_temp = readLine().split(' ');
 var m = parseInt(m_temp[0]);
 var n = parseInt(m_temp[1]);
 magazine = readLine().split(' ');
 ransom = readLine().split(' ');
 var freqs = {}
 for (var i = 0; i < m; i++){
 freqs[magazine[i]] = (freqs[magazine[i]] || 0) + 1;
 }
 var result = "Yes"
 for (var j = 0; j < n; j++){
 if (freqs[ransom[j]] && freqs[ransom[j]] > 0){
 freqs[ransom[j]] -= 1;
 } else {
 result = "No"
 break;
 }
 }
 console.log(result)
}

I wonder if there's a more efficient solution than this? Thanks! I understand forEach could be used for code brevity, but I'm just using for loop for the extra performance benefit (https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead)

asked Jun 14, 2017 at 9:12
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I expect this second for-loop body to execute a tiny bit faster: if (!--freqs[ransom[i]] >= 0){result = "No"; break;} \$\endgroup\$ Commented Jun 14, 2017 at 17:54
  • \$\begingroup\$ @le_m Nice! Your solutions seems to always be highly elegant. \$\endgroup\$ Commented Jun 18, 2017 at 15:25

1 Answer 1

1
\$\begingroup\$

Your solution may work for valid inputs, but you are not checking invalid input situations such as when the ransom letter contains more words than the ones in the magazine:

if(n > m):
 throw new Error("ransom can not be written from magazine");

You can even go further by checking if the first line corresponds to what it pretends to be:

if (magazine.length !== m) 
 throw new Error("Wrong words number in magazine");
if (ransom.length !== n) 
 throw new Error("Wrong words number in ransom");

You can refactor the above conditions in one single line:

if(n > m || agazine.length !== m || ransom.length !== n):
 throw new Error("Invalid input");
answered Jun 14, 2017 at 13:10
\$\endgroup\$
1
  • \$\begingroup\$ Right, edge cases! ty \$\endgroup\$ Commented Jun 18, 2017 at 15:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.