Torna alle sfide
Facile 0 risolte 0% di accettazione

Reverse a String

Return the characters of a string in reverse order.

Cosa imparerai

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

Problema

Mostra traduzione

Reverse text so the last character comes first.

Implement reverse_string(text).

Input

text: The text to reverse.

Output

Return the reversed text.

Vincoli

0 ≤ length ≤ 10,000

Esempi

Esempio 1
Input:
text = "hello"

Output atteso:
expected = "olleh"
Input:
std::string text = "hello";

Output atteso:
std::string expected = "olleh";
Input:
string text = "hello";

Output atteso:
string expected = "olleh";

Spiegazione

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

Esempio 2
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

A single character reversed is itself.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

The answer is the characters in opposite order.

Suggerimento 2

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

Suggerimento 3

Be sure the empty string stays empty after reversal.

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.