Problema
Build a new string from the characters of text at positions 0, 2, 4, … (every other character, starting with the first).
Implement every_other_character(text).
Input
text: The original text.
Output
Return the characters at even positions, in order.
Vincoli
0 ≤ length of text ≤ 10,000
Esempi
Input:
text = "programming"
Output atteso:
expected = "pormig"
Input:
std::string text = "programming";
Output atteso:
std::string expected = "pormig";
Input:
string text = "programming";
Output atteso:
string expected = "pormig";
Spiegazione
Positions 0, 2, 4, 6, 8, 10 give p, o, r, m, i, g: "pormig".
Input:
text = "a"
Output atteso:
expected = "a"
Input:
std::string text = "a";
Output atteso:
std::string expected = "a";
Input:
string text = "a";
Output atteso:
string expected = "a";
Spiegazione
Only position 0 exists: "a".
Come affrontarlo
Prova prima da solo, poi apri un suggerimento alla volta.
Suggerimento 1
Vuoi i caratteri alle posizioni 0, 2, 4 e così via.
Suggerimento 2
Una scansione a passi di due è il modo più semplice per selezionarli.
Suggerimento 3
Se il testo è vuoto o ha lunghezza dispari, il pattern funziona comunque in modo naturale.
Complessità attesa
tempo O(n), spazio aggiuntivo O(n)
Riferimenti
Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.