Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Acronym Generator

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

Bạn sẽ học được gì

  • Tách văn bản theo khoảng trắng và dấu gạch nối
  • Lấy chữ cái đầu của mỗi từ
  • Chuyển chữ cái thành chữ hoa

Đề bài

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.

Ràng buộc

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.

Ví dụ

Ví dụ 1
Đầu vào:
phrase = "central-processing unit"

Kết quả mong đợi:
expected = "CPU"
Đầu vào:
std::string phrase = "central-processing unit";

Kết quả mong đợi:
std::string expected = "CPU";
Đầu vào:
string phrase = "central-processing unit";

Kết quả mong đợi:
string expected = "CPU";

Giải thích

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

Ví dụ 2
Đầu vào:
phrase = "don't repeat yourself"

Kết quả mong đợi:
expected = "DRY"
Đầu vào:
std::string phrase = "don't repeat yourself";

Kết quả mong đợi:
std::string expected = "DRY";
Đầu vào:
string phrase = "don't repeat yourself";

Kết quả mong đợi:
string expected = "DRY";

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Xem khoảng trắng và dấu gạch nối là dấu tách từ, nhưng bỏ qua chữ số và dấu câu khác trong hoặc quanh từ.

Gợi ý 2

Lấy chữ cái đầu tiên sau mỗi dấu tách và đổi sang chữ hoa.

Gợi ý 3

Dấu nháy trong một từ không tạo từ mới, và các đoạn rỗng nên được bỏ qua.

Độ phức tạp mong đợi

thời gian O(n), bộ nhớ phụ O(n)

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.