すべてのチャレンジ
簡単 2件解決済み 正答率100.0%

Replace Spaces With Dashes

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

このチャレンジで学べること

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

問題

翻訳を表示

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.

制約

0 ≤ length of text ≤ 10,000

例

例 1
入力:
text = "Hello World"

期待される出力:
expected = "hello-world"
入力:
std::string text = "Hello World";

期待される出力:
std::string expected = "hello-world";
入力:
string text = "Hello World";

期待される出力:
string expected = "hello-world";

解説

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

例 2
入力:
text = "Learn  To Code"

期待される出力:
expected = "learn--to-code"
入力:
std::string text = "Learn  To Code";

期待される出力:
std::string expected = "learn--to-code";
入力:
string text = "Learn  To Code";

期待される出力:
string expected = "learn--to-code";

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを1つずつ開いてみましょう。

ヒント 1

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

ヒント 2

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

ヒント 3

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

目安の計算量

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

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 LMLaura Mahlow Python 790 ms - 5/5 コードを見る
2 RCRiccardo Corti Python 832 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意: この下に解答が2件あります まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。