問題
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
スペースとハイフンを単語の区切りとして扱い、単語の前後や途中の数字や記号は無視します。
ヒント 2
各区切りの後に来る最初の文字を拾って大文字にします。
ヒント 3
単語内のアポストロフィは新しい単語にせず、空の部分は飛ばします。
目安の計算量
時間 O(n)、追加メモリ O(n)
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。