โจทย์
Replace every space in text with a dash -, and convert all letters to lowercase.
Implement slugify_spaces(text).
Input
text: The text to convert.
Output
Return the converted text.
เงื่อนไข
0 ≤ length of text ≤ 10,000
ตัวอย่าง
อินพุต:
text = "Hello World"
ผลลัพธ์ที่คาดหวัง:
expected = "hello-world"
อินพุต:
std::string text = "Hello World";
ผลลัพธ์ที่คาดหวัง:
std::string expected = "hello-world";
อินพุต:
string text = "Hello World";
ผลลัพธ์ที่คาดหวัง:
string expected = "hello-world";
คำอธิบาย
The space becomes a dash and letters are lowercased: "hello-world".
อินพุต:
text = "Learn To Code"
ผลลัพธ์ที่คาดหวัง:
expected = "learn--to-code"
อินพุต:
std::string text = "Learn To Code";
ผลลัพธ์ที่คาดหวัง:
std::string expected = "learn--to-code";
อินพุต:
string text = "Learn To Code";
ผลลัพธ์ที่คาดหวัง:
string expected = "learn--to-code";
คำอธิบาย
Each space becomes its own dash: "learn--to-code".
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
แปลงสตริงทั้งก้อนโดยทำทั้งการแปลงเป็นตัวพิมพ์เล็กและแทนที่ช่องว่าง
คำใบ้ 2
ใช้การวนผ่านเพียงรอบเดียวเพื่อสร้างผลลัพธ์พร้อมจัดการแต่ละตัวอักษร
คำใบ้ 3
ระวังอย่าเปลี่ยนตัวอักษรอื่นนอกจากช่องว่าง
ความซับซ้อนที่คาดหวัง
เวลา O(n), หน่วยความจำเพิ่ม O(n)
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน