pangrams
HackerRank 問題: pangrams
Question
A pangram is a string that contains every letter of the alphabet. Given a sentence determine whether it is a pangram in the English alphabet. Ignore case. Return either pangram or not pangram as appropriate.
Answer
<?php
/*
* Complete the 'pangrams' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
function pangrams($check_sentence)
{
// initial letter mapping
$all_pangrams_letter_mapping = [];
$ascii_from = 97;
$ascii_to = 122;
for ($ascii_code = $ascii_from; $ascii_code <= $ascii_to; $ascii_code++) {
$letter = chr($ascii_code);
$all_pangrams_letter_mapping[$letter] = true;
}
// check every letter
$total_length_of_sentence = strlen($check_sentence);
for ($letter_index = 0; $letter_index < $total_length_of_sentence; $letter_index++) {
// get letter
$letter = $check_sentence[$letter_index];
// convert to lower case
$letter = strtolower($letter);
unset($all_pangrams_letter_mapping[$letter]);
}
// calculate left letter count
$left_letter_count = count($all_pangrams_letter_mapping);
$check_pangram = 'pangram';
if ($left_letter_count > 0) {
$check_pangram = 'not pangram';
}
return $check_pangram;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$s = rtrim(fgets(STDIN), "\r\n");
$result = pangrams($s);
fwrite($fptr, $result . "\n");
fclose($fptr);