Đề bài
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ụ
Đầ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".
Đầ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.