1
\$\begingroup\$

Not sure if I can ask this question here -

I am working on a problem statement of hacker rank, the idea is that a kidnapper want to recreate a ransom note using a magazine, the input format is as below -

6 4
give me one grand today night
give one grand today

6 and 4 are the counts of words in magazine and ransom respectively, so I just need to check and return "Yes" if the ransom note can be re-created using the words in magazine, otherwise return "No".

My code is working fine as expected, but during two test cases the output is being printed wrong, and that is because of an extra space at the end of a key.

I am first taking the counts of the array values into another array, and checking the value by comparing the keys of ransom with magazine.

I've downloaded the test case and when I test it locally the result is No, that is because the last word in the magazine array is having an extra white space character.

Here's the search where you can see there is an extra space at the end of the string, and because of that the string is not getting matched and is returning output as "No". I've observed that is that last string in the array after counting the values.

Beneath that you have links to the inputs and expected output.

enter image description here

Test case 1: I/P - https://hr-testcases-us-east-1.s3.amazonaws.com/24432/input16.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1513762294&Signature=GoW9OnUfaKo3gwmM2X4L%2FpjQndM%3D&response-content-type=text%2Fplain

Expected O/P: Yes

<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d %d",$m,$n);
$magazine_temp = fgets($handle);
$magazine = explode(" ",$magazine_temp);
$ransom_temp = fgets($handle);
$ransom = explode(" ",$ransom_temp);

    $countRan = array_count_values($ransom);
    $countMag = array_count_values($magazine);

    if(count($countRan) > count($countMag)) {
        print "No";
        return;
    }

    foreach ($countRan as $key => $value){
        // exit if key not exists in $countMag array
        // or exit if value in $countRan[$key] greater than $countMag[$key]
        if (!array_key_exists($key, $countMag) || $value > $countMag[$key]) {
            print "No";
            return;
        }
    }
    print "Yes";
?>
\$\endgroup\$
  • \$\begingroup\$ You can use array_filter() to remove the empty strings that come from blanks at the end. Or use $m and $n along with fscanf() so you read the specified number of words into each array. Or use trim() to remove whitespace at the beginning and end of the lines. \$\endgroup\$ – Barmar Dec 23 '17 at 17:15
  • \$\begingroup\$ I saw this question at Stackoverflow too. Can you explain why "an extra space at the end of a key" is causing trouble? I also don't understand why you are given 6 and 4, when only the words matter. \$\endgroup\$ – mickmackusa Jan 12 '18 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.