모든 챌린지
쉬움 0명 해결 정답률 0%

Word Tokenizer and Vocabulary

Split text into lowercase words and build a vocabulary sorted by frequency, the first step of any language model.

배우게 될 내용

  • Split text into lowercase word tokens
  • Count word frequencies
  • Sort a vocabulary by frequency, then alphabetically

문제

번역본 보기

Language models work with tokens, not raw text. A simple tokenizer lowercases the text and treats every run of letters a-z and digits 0-9 as a word; everything else (spaces, punctuation, other characters) separates words.

Build the vocabulary: the distinct words, sorted by how often they appear (most frequent first), with ties broken alphabetically. A word's position in this list is its token ID.

Implement build_vocabulary(text).

Input

text: Any text.

Output

Return the vocabulary as a list of words. Return an empty list if the text has no words.

제약 조건

0 ≤ length of text ≤ 100,000

예시

예시 1
입력:
text = "The cat sat on the mat. The end!"

예상 출력:
expected = ["the", "cat", "end", "mat", "on", "sat"]
입력:
std::string text = "The cat sat on the mat. The end!";

예상 출력:
std::vector<std::string> expected = {"the", "cat", "end", "mat", "on", "sat"};
입력:
string text = "The cat sat on the mat. The end!";

예상 출력:
string[] expected = new string[] {"the", "cat", "end", "mat", "on", "sat"};

설명

Lowercased, the words are the, cat, sat, on, the, mat, the, end. "the" appears 3 times; the rest appear once and are sorted alphabetically: ["the", "cat", "end", "mat", "on", "sat"].

예시 2
입력:
text = "AI, ai and A.I. (2024)"

예상 출력:
expected = ["ai", "2024", "a", "and", "i"]
입력:
std::string text = "AI, ai and A.I. (2024)";

예상 출력:
std::vector<std::string> expected = {"ai", "2024", "a", "and", "i"};
입력:
string text = "AI, ai and A.I. (2024)";

예상 출력:
string[] expected = new string[] {"ai", "2024", "a", "and", "i"};

설명

Words: ai, ai, and, a, i, 2024. "ai" appears twice; the others once, sorted alphabetically with digits before letters: ["ai", "2024", "a", "and", "i"].

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

First extract only runs of letters and digits, lowercased, and ignore every other character.

힌트 2

Count how often each word appears, then sort by frequency descending and alphabetically for ties.

힌트 3

If the text contains no valid words, return an empty list instead of a singleton or null result.

예상 복잡도

O(n log k) time, O(k) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.