Problema
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.
Vincoli
0 ≤ length of text ≤ 10,000
Esempi
Input:
text = "Hello World"
Output atteso:
expected = "hello-world"
Input:
std::string text = "Hello World";
Output atteso:
std::string expected = "hello-world";
Input:
string text = "Hello World";
Output atteso:
string expected = "hello-world";
Spiegazione
The space becomes a dash and letters are lowercased: "hello-world".
Input:
text = "Learn To Code"
Output atteso:
expected = "learn--to-code"
Input:
std::string text = "Learn To Code";
Output atteso:
std::string expected = "learn--to-code";
Input:
string text = "Learn To Code";
Output atteso:
string expected = "learn--to-code";
Spiegazione
Each space becomes its own dash: "learn--to-code".
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Transform the whole string by applying both lowercase conversion and space replacement.
Suggerimento 2
A single pass can build the result while handling each character.
Suggerimento 3
Be careful not to change non-letter characters except spaces.
Complessità attesa
O(n) time, O(n) extra space
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.