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.
pangram 是一個字串包含字母表中每個字母,給予一個句子,請判斷他是否是英文字母表中的 pangram。請忽略大小寫,根據需要回傳 pangram 或 not pangram。
Example 1:
s = "The quick brown fox jumps over the lazy dog" The string contains all letters in the English alphabet, so return pangram.
solution:
先將 input 的 s 轉成小寫的狀態,接著運用 new Set 的特性搭配展開運算(…),
將 s 做成不重複的字母陣列,最後確認這個長度是否包含 26 個英文字母,
注意,空白仍計算在內,故為 27 時,符合條件回傳 “pangram”。
Code 1: BigO(n)
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'pangrams' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING s as parameter.
*/
function pangrams(s) {
// Write your code here
let lowerCaseInput = s.toLowerCase()
let uniqueChars = [...new Set(lowerCaseInput)]
//Including space
if (uniqueChars.length === 27) {
return "pangram";
} else {
return "not pangram";
}
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const s = readLine();
const result = pangrams(s);
ws.write(result + '\n');
ws.end();
}
FlowChart:
Example 1
s = "The quick brown fox jumps over the lazy dog" uniqueChars = [ 't', 'h', 'e', ' ', 'q', 'u', 'i', 'c', 'k', 'b', 'r', 'o', 'w', 'n', 'f', 'x', 'j', 'm', 'p', 's', 'v', 'l', 'a', 'z', 'y', 'd', 'g' ] uniqueChars.length = 27 return pangram;