問題
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.
例
入力:
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.
入力:
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つずつ開いてみましょう。
ヒント 1
Treat spaces and hyphens as word separators, but ignore digits and other punctuation within or around words.
ヒント 2
Look for the first letter after each separator and uppercase it.
ヒント 3
Apostrophes inside a word should not create a new word, and empty chunks should be skipped.
目安の計算量
O(n) time, O(n) extra space
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。