โจทย์
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
ข้ามตัวอักษรและตัวเลขที่ไม่เกี่ยวก่อนเทียบปลายทั้งสอง
คำใบ้ 2
เทียบอักขระด้านนอกก่อน แล้วค่อยรีเคอร์ซีฟกับส่วนกลางที่เหลือ
คำใบ้ 3
กรณีสตริงที่กรองแล้วว่างหรือเหลือ 1 ตัว ให้นับว่าเป็นพาลินโดรม
ความซับซ้อนที่คาดหวัง
เวลา O(n), หน่วยความจำเพิ่ม O(n)
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน