[boj] 1157. 単語学習(node.js)
7696 ワード
サマリ入力文字列で最も頻度の高い文字を返します. 入力値は大文字と小文字を区別せず、出力値は大文字とする. このときに現れる頻度が同じ文字が複数あるとしたら?返却(63). に答える
説明する くどくど言うjavascriptではすでに様々な方法が実現されているので驚きです.効果的に適切な場所で使うことが大切です.
説明する
const fs = require("fs");
const filePath = process.platform === "linux" ? "dev/stdin" : "input.txt";
const stdin = fs.readFileSync(filePath).toString().split("\n");
let cnt = 0;
const input = () => {
return stdin[cnt++];
};
const solution = () => {
const count = new Array(26);
count.fill(0);
input()
.toLowerCase()
.split("")
.map((x) => count[x.charCodeAt() - 97]++);
let answer = 0;
const max = Math.max.apply(null, count);
const originalIdx = count.indexOf(max);
const lastIdx = count.lastIndexOf(max);
if (originalIdx == lastIdx) {
answer = originalIdx;
} else {
answer = 63 - 97;
}
console.log(String.fromCharCode(answer + 97).toUpperCase());
};
solution();
覚えておきたいString.fromCharCode(숫자)
: int to str 문자.charCodeAt()
: str to int array.fill(0)
埋め立てarray.indexOf(값)
配列インデックスの返却array.lastIndexOf(값)
インデックスを並べて検索を開始して戻るString.toLowerCase()
・String.toUpperCase()
文字列全体を大文字小文字に変換Reference
この問題について([boj] 1157. 単語学習(node.js)), 我々は、より多くの情報をここで見つけました https://velog.io/@greenish0902/boj-1157.-단어-공부-node.jsテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol