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

Palindrome String

Check whether text reads the same forwards and backwards.

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

  • Compare characters from both ends
  • Stop early when a mismatch appears
  • Handle empty and one-character strings

โจทย์

ดูฉบับแปล

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

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
text = "Racecar"

ผลลัพธ์ที่คาดหวัง:
expected = True
อินพุต:
std::string text = "Racecar";

ผลลัพธ์ที่คาดหวัง:
bool expected = true;
อินพุต:
string text = "Racecar";

ผลลัพธ์ที่คาดหวัง:
bool expected = true;

คำอธิบาย

Lowercased, "racecar" reads the same both ways.

ตัวอย่างที่ 2
อินพุต:
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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

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

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

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