Problème
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.
Contraintes
0 ≤ length of text ≤ 10,000
Exemples
Entrée :
text = "programming"
Sortie attendue :
expected = "pormig"
Entrée :
std::string text = "programming";
Sortie attendue :
std::string expected = "pormig";
Entrée :
string text = "programming";
Sortie attendue :
string expected = "pormig";
Explication
Positions 0, 2, 4, 6, 8, 10 give p, o, r, m, i, g: "pormig".
Entrée :
text = "a"
Sortie attendue :
expected = "a"
Entrée :
std::string text = "a";
Sortie attendue :
std::string expected = "a";
Entrée :
string text = "a";
Sortie attendue :
string expected = "a";
Explication
Only position 0 exists: "a".
Comment l'aborder
Essayez d'abord seul, puis ouvrez les indices un par un.
Indice 1
Tu veux les caractères aux positions 0, 2, 4, et ainsi de suite.
Indice 2
Un parcours avec un pas de deux est la façon la plus simple de les sélectionner.
Indice 3
Si le texte est vide ou de longueur impaire, le motif fonctionne quand même naturellement.
Complexité attendue
temps O(n), espace supplémentaire O(n)
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.