문제
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
공백과 하이픈은 단어 구분자로 보고, 단어 안팎의 숫자와 다른 구두점은 무시하세요.
힌트 2
각 구분자 뒤의 첫 글자를 찾아 대문자로 바꾸세요.
힌트 3
단어 안의 아포스트로피는 새 단어로 보지 말고, 빈 조각은 건너뛰세요.
예상 복잡도
시간 O(n), 추가 공간 O(n)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.