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
You want characters at positions 0, 2, 4, and so on.
Indice 2
A step-by-two traversal is the simplest way to select them.
Indice 3
If the text is empty or has odd length, the pattern still works naturally.
Complexité attendue
O(n) time, O(n) extra space
Références
Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.