Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Palindrome String

Check whether text reads the same forwards and backwards.

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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ụ

Ví dụ 1
Đầ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.

Ví dụ 2
Đầ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.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.