問題
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
例
入力:
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.
入力:
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
両端を比べる前に、文字でも数字でもないものは飛ばします。
ヒント 2
外側の文字を先に比較し、残った中央部分に対して再帰します。
ヒント 3
空文字列と1文字だけの整形後文字列は回文として扱います。
目安の計算量
時間 O(n)、追加メモリ O(n)
参考資料
この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。