Tutte le sfide
Media 0 risolte 0% di accettazione

Number Letter Counts

Count the letters needed to write every number from 1 to n in English words.

Cosa imparerai

  • Spell numbers in English words
  • Split a number into thousands, hundreds, tens and ones
  • Count letters while ignoring spaces and hyphens

Problema

Mostra traduzione

A sign maker spells out house numbers in words and pays by the letter. Write every number from 1 to n in American English, without the word "and": 342 is "three hundred forty-two" and 2005 is "two thousand five". Spaces and hyphens are free; only letters count.

Implement number_letter_counts(n).

Spellings: one to nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand. Numbers from 1,000 up are written as the thousands part (itself in words, up to ninety-nine) followed by "thousand" and then the rest, if any.

Input

n: The last number to write.

Output

Return the total number of letters used for 1, 2, ..., n. For n = 0 nothing is written and the answer is 0.

Vincoli

0 ≤ n ≤ 99,999

Esempi

Esempio 1
Input:
n = 3

Output atteso:
expected = 11
Input:
long long n = 3;

Output atteso:
long long expected = 11;
Input:
long n = 3;

Output atteso:
long expected = 11;

Spiegazione

"one", "two" and "three" have 3 + 3 + 5 = 11 letters.

Esempio 2
Input:
n = 21

Output atteso:
expected = 121
Input:
long long n = 21;

Output atteso:
long long expected = 121;
Input:
long n = 21;

Output atteso:
long expected = 121;

Spiegazione

1 to 19 use 106 letters, twenty adds 6 and twenty-one adds 9, for 121.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Handle 1 to 19 with a table, then the tens words for 20 to 90.

Suggerimento 2

A three-digit block is the hundreds word plus "hundred" plus the last two digits.

Suggerimento 3

For numbers of 1,000 and up, spell the thousands block, add "thousand", then spell the rest.

Complessità attesa

O(n) time, O(1) extra space

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.