Đề 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
Bỏ qua ký tự không phải chữ hoặc số trước khi so sánh hai đầu.
Gợi ý 2
So sánh ký tự ở ngoài trước, rồi đệ quy trên phần giữa còn lại.
Gợi ý 3
Xử lý chuỗi đã lọc rỗng hoặc chỉ có một ký tự như palindrome.
Độ phức tạp mong đợi
thời gian O(n), bộ nhớ phụ O(n)
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.