Live-Prüfung
Two-Pointer O(1)
Ergebnis:
O(1) Auxiliary Space
✓ ECHTES PALINDROM
Code-Gegenüberstellung
Audit
Naiver 1st-Shot (O(N) RAM, löscht Umlaute)
import re
def is_palindrome(s: str) -> bool:
- clean = re.sub(r'[^a-zA-Z0-9]', '', s).lower() # Löscht ä, ö, ü!
- return clean == clean[::-1] # Verdoppelt RAM (O(N))
In-Place Two-Pointer (O(1) RAM, Unicode)
def is_palindrome(s: str) -> bool:
left, right = 0, len(s) - 1
while left < right:
while left < right and not s[left].isalnum(): left += 1
while left < right and not s[right].isalnum(): right -= 1
if s[left].casefold() != s[right].casefold():
return False
left += 1; right -= 1
return True
14/14 Pytests PASSED
pytest test_palindrom.py -v
| Test | Input | Erwartung | Status |
|---|---|---|---|
| test_empty_string | "" | True | PASSED |
| test_german_umlaute | "Na, Freibierfan?" | True | PASSED |
| test_panama_sentence | "A man, a plan, a canal, Panama" | True | PASSED |
| test_non_palindrome | "Not a palindrome" | False | PASSED |