Tutte le sfide
Facile 2 risolte 100.0% di accettazione

Replace Spaces With Dashes

Turn spaces into dashes to make a URL-style slug.

Cosa imparerai

  • Replace characters while copying a string
  • Build a URL-style slug
  • Compare manual loops with replace methods

Problema

Mostra traduzione

Replace every space in text with a dash -, and convert all letters to lowercase.

Implement slugify_spaces(text).

Input

text: The text to convert.

Output

Return the converted text.

Vincoli

0 ≤ length of text ≤ 10,000

Esempi

Esempio 1
Input:
text = "Hello World"

Output atteso:
expected = "hello-world"
Input:
std::string text = "Hello World";

Output atteso:
std::string expected = "hello-world";
Input:
string text = "Hello World";

Output atteso:
string expected = "hello-world";

Spiegazione

The space becomes a dash and letters are lowercased: "hello-world".

Esempio 2
Input:
text = "Learn  To Code"

Output atteso:
expected = "learn--to-code"
Input:
std::string text = "Learn  To Code";

Output atteso:
std::string expected = "learn--to-code";
Input:
string text = "Learn  To Code";

Output atteso:
string expected = "learn--to-code";

Spiegazione

Each space becomes its own dash: "learn--to-code".

Come affrontarlo

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

Suggerimento 1

Transform the whole string by applying both lowercase conversion and space replacement.

Suggerimento 2

A single pass can build the result while handling each character.

Suggerimento 3

Be careful not to change non-letter characters except spaces.

Complessità attesa

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

Riferimenti

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

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 LMLaura Mahlow Python 790 ms - 5/5 Vedi il codice
2 RCRiccardo Corti Python 832 ms - 5/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 2 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.