ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Word Tile Score

Add the tile values for every letter in a word.

สิ่งที่คุณจะได้เรียนรู้

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

โจทย์

ดูฉบับแปล

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.

เงื่อนไข

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

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
word = "code"

ผลลัพธ์ที่คาดหวัง:
expected = 7
อินพุต:
std::string word = "code";

ผลลัพธ์ที่คาดหวัง:
long long expected = 7;
อินพุต:
string word = "code";

ผลลัพธ์ที่คาดหวัง:
long expected = 7;

คำอธิบาย

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

ตัวอย่างที่ 2
อินพุต:
word = "Quiz"

ผลลัพธ์ที่คาดหวัง:
expected = 22
อินพุต:
std::string word = "Quiz";

ผลลัพธ์ที่คาดหวัง:
long long expected = 22;
อินพุต:
string word = "Quiz";

ผลลัพธ์ที่คาดหวัง:
long expected = 22;

คำอธิบาย

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

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

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

คำใบ้ 2

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

คำใบ้ 3

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

ความซับซ้อนที่คาดหวัง

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

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก