Tutte le sfide
Facile 0 risolte 0% di accettazione

Word Tile Score

Add the tile values for every letter in a word.

Cosa imparerai

  • Store letter scores in a dictionary
  • Add up values for every letter
  • Ignore letter case

Problema

Mostra traduzione

Add the tile values for every letter in a word. Ignore case. There are no board multipliers or wildcard tiles.

Letter values:

A, E, I, O, U, L, N, R, S, T: 1 point each.

D, G: 2 each.

B, C, M, P: 3 each.

F, H, V, W, Y: 4 each.

K: 5. J, X: 8 each. Q, Z: 10 each.

Implement word_tile_score(word).

Input

word: The word to score.

Output

Return the sum of all letter values.

Vincoli

0 ≤ word length ≤ 10,000. Only ASCII letters are allowed. Empty input scores 0.

Esempi

Esempio 1
Input:
word = "code"

Output atteso:
expected = 7
Input:
std::string word = "code";

Output atteso:
long long expected = 7;
Input:
string word = "code";

Output atteso:
long expected = 7;

Spiegazione

C is worth 3, O is 1, D is 2, and E is 1: 3 + 1 + 2 + 1 = 7.

Esempio 2
Input:
word = "Quiz"

Output atteso:
expected = 22
Input:
std::string word = "Quiz";

Output atteso:
long long expected = 22;
Input:
string word = "Quiz";

Output atteso:
long expected = 22;

Spiegazione

Case does not matter. Q + U + I + Z gives 10 + 1 + 1 + 10 = 22.

Come affrontarlo

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

Suggerimento 1

Each letter contributes a fixed value, so score the word by summing per-character weights.

Suggerimento 2

A case-insensitive lookup table makes the mapping simple and fast.

Suggerimento 3

Make sure every allowed letter is covered, including the high-value Q and Z tiles.

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.