โจทย์
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".
เงื่อนไข
1 ≤ length of full_name ≤ 200; it contains at least one letter
ตัวอย่าง
อินพุต:
full_name = "Ada Lovelace"
ผลลัพธ์ที่คาดหวัง:
expected = "AL"
อินพุต:
std::string full_name = "Ada Lovelace";
ผลลัพธ์ที่คาดหวัง:
std::string expected = "AL";
อินพุต:
string full_name = "Ada Lovelace";
ผลลัพธ์ที่คาดหวัง:
string expected = "AL";
คำอธิบาย
A from Ada and L from Lovelace: "AL".
อินพุต:
full_name = "grace brewster hopper"
ผลลัพธ์ที่คาดหวัง:
expected = "GBH"
อินพุต:
std::string full_name = "grace brewster hopper";
ผลลัพธ์ที่คาดหวัง:
std::string expected = "GBH";
อินพุต:
string full_name = "grace brewster hopper";
ผลลัพธ์ที่คาดหวัง:
string expected = "GBH";
คำอธิบาย
Extra spaces don't matter; the initials are uppercased: "GBH".
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
แต่ละคำให้มาเพียงหนึ่งตัวอักษร คืออักษรตัวแรกของคำนั้น
คำใบ้ 2
ไล่ผ่านชื่อโดยจับจังหวะที่เริ่มคำใหม่หลังช่องว่าง
คำใบ้ 3
ระวังช่องว่างติดกันหลายตัว เพื่อให้เอาแค่ initial เดียวต่อหนึ่งคำ
ความซับซ้อนที่คาดหวัง
เวลา O(n), หน่วยความจำเพิ่ม O(1)
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน