簡単 0件解決済み 正答率0%

Palindrome String

Check whether text reads the same forwards and backwards.

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

  • Compare characters from both ends
  • Stop early when a mismatch appears
  • Handle empty and one-character strings

問題

翻訳を表示

Decide whether text is a palindrome, considering only letters and digits and ignoring case.

Implement is_palindrome(text).

Input

text: The text to check (ASCII characters).

Output

Return true for a palindrome, otherwise false.

制約

0 ≤ length ≤ 10,000

例

例 1
入力:
text = "Racecar"

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

期待される出力:
bool expected = true;
入力:
string text = "Racecar";

期待される出力:
bool expected = true;

解説

Lowercased, "racecar" reads the same both ways.

例 2
入力:
text = "hello"

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

期待される出力:
bool expected = false;
入力:
string text = "hello";

期待される出力:
bool expected = false;

解説

"hello" backwards is "olleh", which is different.

解き方のヒント

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

ヒント 1

Ignore anything that is not a letter or digit before comparing.

ヒント 2

Move inward from both ends, comparing case-insensitive characters.

ヒント 3

Skip punctuation and spaces carefully so they do not break valid palindromes.

目安の計算量

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

参考資料

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

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。