中級 3件解決済み 正答率100.0%

Recursive Palindrome

Check a palindrome recursively, ignoring case and punctuation.

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

  • Check palindromes recursively
  • Ignore case and punctuation
  • Shrink the problem from both ends

問題

翻訳を表示

Decide whether text reads the same forwards and backwards, considering only letters and digits and ignoring case. Solve it recursively: compare the outer characters, then check the inside.

Implement is_palindrome_recursive(text).

Input

text: The text to check (ASCII characters).

Output

Return true for a palindrome, otherwise false.

制約

0 ≤ length ≤ 1,000

例

例 1
入力:
text = "Never odd or even"

期待される出力:
expected = True
入力:
std::string text = "Never odd or even";

期待される出力:
bool expected = true;
入力:
string text = "Never odd or even";

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

解説

Letters only, lowercased: "neveroddoreven" reads the same both ways.

例 2
入力:
text = "code"

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

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

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

解説

"c" and "e" differ, so it is not a palindrome.

解き方のヒント

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

ヒント 1

Skip non-letter and non-digit characters before comparing the ends.

ヒント 2

Compare the outer characters first, then recurse on the remaining middle part.

ヒント 3

Handle empty and single-character cleaned substrings as palindromes.

目安の計算量

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

参考資料

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

ランキング

全体ランキング

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

# コーダー 言語 時間 メモリ AI 評価 解答
1 LMLaura Mahlow Python 962 ms - 4/5 コードを見る
2 RCRiccardo Corti Python 965 ms - 4/5 コードを見る
3 Н"Никита "Programmer" Python 983 ms - 5/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

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