150強訪談簡單

有效回文

「有效回文」問題的詳細指南和 Python 實作。

問題陳述

簡單

如果一個短語在將所有大寫字母轉換為小寫字母並刪除所有非字母數字字元後,向前和向後讀取相同的內容,則該短語是回文。字母數字字元包括字母和數字。

給定一個字串 s,如果它是回文,則傳回 True,否則傳回 False

寫一個函數 isPalindrome(s: str) -> bool

約束條件
  • 1 <= len(s) <= 2 * 10^5
  • s consists only of printable ASCII characters

範例

Example 1
Input
s = "A man, a plan, a canal: Panama"
Output
True
Explanation

After removing non-alphanumeric characters and converting to lowercase: "amanaplanacanalpanama", which is a palindrome.

Example 2
Input
s = "race a car"
Output
False
Explanation

After processing: "raceacar" is not a palindrome.

Example 3
Input
s = " "
Output
True
Explanation

After removing non-alphanumeric characters, s is an empty string. An empty string is a palindrome by definition.

Need a Hint?
考慮使用特定於兩個指標的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。