Đề bài
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.
Ràng buộc
0 ≤ length ≤ 10,000
Ví dụ
Đầu vào:
text = "Racecar"
Kết quả mong đợi:
expected = True
Đầu vào:
std::string text = "Racecar";
Kết quả mong đợi:
bool expected = true;
Đầu vào:
string text = "Racecar";
Kết quả mong đợi:
bool expected = true;
Giải thích
Lowercased, "racecar" reads the same both ways.
Đầu vào:
text = "hello"
Kết quả mong đợi:
expected = False
Đầu vào:
std::string text = "hello";
Kết quả mong đợi:
bool expected = false;
Đầu vào:
string text = "hello";
Kết quả mong đợi:
bool expected = false;
Giải thích
"hello" backwards is "olleh", which is different.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
Ignore anything that is not a letter or digit before comparing.
Gợi ý 2
Move inward from both ends, comparing case-insensitive characters.
Gợi ý 3
Skip punctuation and spaces carefully so they do not break valid palindromes.
Độ phức tạp mong đợi
O(n) time, O(1) extra space
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.