Fácil 0 resueltos 0% de aceptación

Reverse a String

Return the characters of a string in reverse order.

Lo que aprenderás

  • Walk through a string backwards
  • Build a new string from characters
  • Use slicing or two pointers

Problema

Ver traducción

Reverse text so the last character comes first.

Implement reverse_string(text).

Input

text: The text to reverse.

Output

Return the reversed text.

Restricciones

0 ≤ length ≤ 10,000

Ejemplos

Ejemplo 1
Entrada:
text = "hello"

Salida esperada:
expected = "olleh"
Entrada:
std::string text = "hello";

Salida esperada:
std::string expected = "olleh";
Entrada:
string text = "hello";

Salida esperada:
string expected = "olleh";

Explicación

h-e-l-l-o read backwards is "olleh".

Ejemplo 2
Entrada:
text = "a"

Salida esperada:
expected = "a"
Entrada:
std::string text = "a";

Salida esperada:
std::string expected = "a";
Entrada:
string text = "a";

Salida esperada:
string expected = "a";

Explicación

A single character reversed is itself.

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

The answer is the characters in opposite order.

Pista 2

A two-pointer approach or a backward traversal both work well.

Pista 3

Be sure the empty string stays empty after reversal.

Complejidad esperada

O(n) time, O(n) extra space

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.