Đề bài
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.
Ràng buộc
0 ≤ length of text ≤ 10,000
Ví dụ
Đầu vào:
text = "Hello World"
Kết quả mong đợi:
expected = "hello-world"
Đầu vào:
std::string text = "Hello World";
Kết quả mong đợi:
std::string expected = "hello-world";
Đầu vào:
string text = "Hello World";
Kết quả mong đợi:
string expected = "hello-world";
Giải thích
The space becomes a dash and letters are lowercased: "hello-world".
Đầu vào:
text = "Learn To Code"
Kết quả mong đợi:
expected = "learn--to-code"
Đầu vào:
std::string text = "Learn To Code";
Kết quả mong đợi:
std::string expected = "learn--to-code";
Đầu vào:
string text = "Learn To Code";
Kết quả mong đợi:
string expected = "learn--to-code";
Giải thích
Each space becomes its own dash: "learn--to-code".
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Transform the whole string by applying both lowercase conversion and space replacement.
Gợi ý 2
A single pass can build the result while handling each character.
Gợi ý 3
Be careful not to change non-letter characters except spaces.
Độ phức tạp mong đợi
O(n) time, O(n) 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.