簡単 1件解決済み 正答率100.0%

Every Other Character

Keep the characters at even positions.

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

  • Pick characters at even positions
  • Loop with a step of 2 or use slicing
  • Build a new string

問題

翻訳を表示

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.

制約

0 ≤ length of text ≤ 10,000

例

例 1
入力:
text = "programming"

期待される出力:
expected = "pormig"
入力:
std::string text = "programming";

期待される出力:
std::string expected = "pormig";
入力:
string text = "programming";

期待される出力:
string expected = "pormig";

解説

Positions 0, 2, 4, 6, 8, 10 give p, o, r, m, i, g: "pormig".

例 2
入力:
text = "a"

期待される出力:
expected = "a"
入力:
std::string text = "a";

期待される出力:
std::string expected = "a";
入力:
string text = "a";

期待される出力:
string expected = "a";

解説

Only position 0 exists: "a".

解き方のヒント

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

ヒント 1

You want characters at positions 0, 2, 4, and so on.

ヒント 2

A step-by-two traversal is the simplest way to select them.

ヒント 3

If the text is empty or has odd length, the pattern still works naturally.

目安の計算量

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

参考資料

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

ランキング

全体ランキング

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

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

他の人の解答から学ぶ

コミュニティの解答

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