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

Acronym Generator

Create an acronym from the first letter of every word, in uppercase.

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

  • แยกข้อความตามช่องว่างและขีดกลาง
  • ใช้ตัวอักษรแรกของแต่ละคำ
  • แปลงตัวอักษรเป็นตัวพิมพ์ใหญ่

โจทย์

Create an acronym from the first letter of every word, in uppercase. Spaces and hyphens separate words. Remove every other punctuation mark and digit without adding a separator; an apostrophe inside a word does not start another word. Ignore empty pieces.

Implement acronym_generator(phrase).

Input

phrase: The phrase to shorten.

Output

Return the uppercase initials in their original order.

เงื่อนไข

0 ≤ phrase length ≤ 10,000. Input contains ASCII letters, digits, spaces, and punctuation; no tabs or line breaks. A phrase with no letters returns empty text.

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
phrase = "central-processing unit"

ผลลัพธ์ที่คาดหวัง:
expected = "CPU"
อินพุต:
std::string phrase = "central-processing unit";

ผลลัพธ์ที่คาดหวัง:
std::string expected = "CPU";
อินพุต:
string phrase = "central-processing unit";

ผลลัพธ์ที่คาดหวัง:
string expected = "CPU";

คำอธิบาย

The hyphen and space separate central, processing, and unit. Their initials are C, P, and U.

ตัวอย่างที่ 2
อินพุต:
phrase = "don't repeat yourself"

ผลลัพธ์ที่คาดหวัง:
expected = "DRY"
อินพุต:
std::string phrase = "don't repeat yourself";

ผลลัพธ์ที่คาดหวัง:
std::string expected = "DRY";
อินพุต:
string phrase = "don't repeat yourself";

ผลลัพธ์ที่คาดหวัง:
string expected = "DRY";

คำอธิบาย

Remove the apostrophe to obtain dont. It is still one word, followed by repeat and yourself.

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

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

คำใบ้ 1

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

คำใบ้ 2

หาตัวอักษรตัวแรกหลังตัวคั่นแต่ละตัว แล้วแปลงเป็นตัวพิมพ์ใหญ่

คำใบ้ 3

เครื่องหมาย apostrophes ในคำเดียวกันไม่ควรเริ่มคำใหม่ และให้ข้ามช่องว่างที่ว่างเปล่า

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

เวลา O(n), หน่วยความจำเพิ่ม O(n)

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

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

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

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

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