ปานกลาง 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Recursive Palindrome

Check a palindrome recursively, ignoring case and punctuation.

สิ่งที่คุณจะได้เรียนรู้

  • ตรวจพาลินโดรมด้วย recursion
  • ไม่สนตัวพิมพ์และเครื่องหมายวรรคตอน
  • ลดขนาดปัญหาจากทั้งสองด้าน

โจทย์

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

ข้ามตัวอักษรและตัวเลขที่ไม่เกี่ยวก่อนเทียบปลายทั้งสอง

คำใบ้ 2

เทียบอักขระด้านนอกก่อน แล้วค่อยรีเคอร์ซีฟกับส่วนกลางที่เหลือ

คำใบ้ 3

กรณีสตริงที่กรองแล้วว่างหรือเหลือ 1 ตัว ให้นับว่าเป็นพาลินโดรม

ความซับซ้อนที่คาดหวัง

เวลา O(n), หน่วยความจำเพิ่ม O(n)

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก