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

Initials From a Name

Build uppercase initials from a full name.

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

  • Split a name into words
  • Take the first letter of each word
  • Join and uppercase the result

Đề bài

Xem bản dịch

Take the first letter of each word in full_name, uppercase it, and join the letters. Words are separated by one or more spaces.

Implement initials(full_name).

Input

full_name: A name made of letters and spaces.

Output

Return the initials, for example "ADL".

Ràng buộc

1 ≤ length of full_name ≤ 200; it contains at least one letter

Ví dụ

Ví dụ 1
Đầu vào:
full_name = "Ada Lovelace"

Kết quả mong đợi:
expected = "AL"
Đầu vào:
std::string full_name = "Ada Lovelace";

Kết quả mong đợi:
std::string expected = "AL";
Đầu vào:
string full_name = "Ada Lovelace";

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

Giải thích

A from Ada and L from Lovelace: "AL".

Ví dụ 2
Đầu vào:
full_name = "grace  brewster  hopper"

Kết quả mong đợi:
expected = "GBH"
Đầu vào:
std::string full_name = "grace  brewster  hopper";

Kết quả mong đợi:
std::string expected = "GBH";
Đầu vào:
string full_name = "grace  brewster  hopper";

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

Giải thích

Extra spaces don't matter; the initials are uppercased: "GBH".

Hướng tiếp cận

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

Gợi ý 1

Each word contributes exactly one character: its first letter.

Gợi ý 2

Traverse the name while tracking when a new word starts after spaces.

Gợi ý 3

Watch for multiple spaces in a row so you only take one initial per word.

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

O(n) time, O(1) extra space

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.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 LMLaura Mahlow Python 717 ms - 5/5 Xem code

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

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

Cảnh báo: Có 1 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem mã của người khác và đánh giá AI. Đăng nhập và giải thử thách để tham gia thảo luận.