Trung bình 3 người đã giải Tỷ lệ chấp nhận 100.0%

Recursive Palindrome

Check a palindrome recursively, ignoring case and punctuation.

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

  • Check palindromes recursively
  • Ignore case and punctuation
  • Shrink the problem from both ends

Đề bài

Xem bản dịch

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.

Ràng buộc

0 ≤ length ≤ 1,000

Ví dụ

Ví dụ 1
Đầu vào:
text = "Never odd or even"

Kết quả mong đợi:
expected = True
Đầu vào:
std::string text = "Never odd or even";

Kết quả mong đợi:
bool expected = true;
Đầu vào:
string text = "Never odd or even";

Kết quả mong đợi:
bool expected = true;

Giải thích

Letters only, lowercased: "neveroddoreven" reads the same both ways.

Ví dụ 2
Đầu vào:
text = "code"

Kết quả mong đợi:
expected = False
Đầu vào:
std::string text = "code";

Kết quả mong đợi:
bool expected = false;
Đầu vào:
string text = "code";

Kết quả mong đợi:
bool expected = false;

Giải thích

"c" and "e" differ, so it is not a palindrome.

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

Skip non-letter and non-digit characters before comparing the ends.

Gợi ý 2

Compare the outer characters first, then recurse on the remaining middle part.

Gợi ý 3

Handle empty and single-character cleaned substrings as palindromes.

Độ phức tạp mong đợi

O(n) time, O(n) 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.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

# Người giải Ngôn ngữ Thời gian Bộ nhớ AI đánh giá Lời giải
1 LMLaura Mahlow Python 962 ms - 4/5 Xem code
2 RCRiccardo Corti Python 965 ms - 4/5 Xem code
3 Н"Никита "Programmer" Python 983 ms - 5/5 Xem code

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

Lời giải cộng đồng

Cảnh báo: Có 3 lời giải đầy đủ bên dưới Hãy thử tự giải trước. Bạn vẫn có thể xem code của người khác và nhận xét của AI. Đăng nhập và giải thử thách để tham gia thảo luận.