โจทย์
Decide whether text is a palindrome, considering only letters and digits and ignoring case.
Implement is_palindrome(text).
Input
text: The text to check (ASCII characters).
Output
Return true for a palindrome, otherwise false.
เงื่อนไข
0 ≤ length ≤ 10,000
ตัวอย่าง
อินพุต:
text = "Racecar"
ผลลัพธ์ที่คาดหวัง:
expected = True
อินพุต:
std::string text = "Racecar";
ผลลัพธ์ที่คาดหวัง:
bool expected = true;
อินพุต:
string text = "Racecar";
ผลลัพธ์ที่คาดหวัง:
bool expected = true;
คำอธิบาย
Lowercased, "racecar" reads the same both ways.
อินพุต:
text = "hello"
ผลลัพธ์ที่คาดหวัง:
expected = False
อินพุต:
std::string text = "hello";
ผลลัพธ์ที่คาดหวัง:
bool expected = false;
อินพุต:
string text = "hello";
ผลลัพธ์ที่คาดหวัง:
bool expected = false;
คำอธิบาย
"hello" backwards is "olleh", which is different.
แนวทางการแก้
ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ
คำใบ้ 1
Ignore anything that is not a letter or digit before comparing.
คำใบ้ 2
Move inward from both ends, comparing case-insensitive characters.
คำใบ้ 3
Skip punctuation and spaces carefully so they do not break valid palindromes.
ความซับซ้อนที่คาดหวัง
O(n) time, O(1) extra space
แหล่งอ้างอิง
โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน