Đề bài
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.
Ràng buộc
0 ≤ length ≤ 1,000
Ví dụ
Đầu vào:
text = "Never odd or even"
Kết quả mong đợi:
expected = True
Đầu vào:
std::string text = "Never odd or even";
Kết quả mong đợi:
bool expected = true;
Đầu vào:
string text = "Never odd or even";
Kết quả mong đợi:
bool expected = true;
Giải thích
Letters only, lowercased: "neveroddoreven" reads the same both ways.
Đầu vào:
text = "code"
Kết quả mong đợi:
expected = False
Đầu vào:
std::string text = "code";
Kết quả mong đợi:
bool expected = false;
Đầu vào:
string text = "code";
Kết quả mong đợi:
bool expected = false;
Giải thích
"c" and "e" differ, so it is not a palindrome.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Skip non-letter and non-digit characters before comparing the ends.
Gợi ý 2
Compare the outer characters first, then recurse on the remaining middle part.
Gợi ý 3
Handle empty and single-character cleaned substrings as palindromes.
Độ phức tạp mong đợi
O(n) time, O(n) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.