> For the complete documentation index, see [llms.txt](https://simian114.gitbook.io/blog/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://simian114.gitbook.io/blog/undefined/undefined-1/01.-leetcode-125.md).

# 01. 유효한 팰린드롬(leetcode: 125)

{% hint style="info" %}
[**정규표현식 패턴**](https://brownbears.tistory.com/62)

[**점프 투 파이썬 정규표현식**](https://wikidocs.net/4308)

[**re**](https://app.gitbook.com/s/-MO1zujRJyNAUnlhg0GB/undefined/undefined-1/ggdocs.python.org/3/library/re.html#re.sub)

[**re sub**](https://docs.python.org/3/library/re.html#re.sub)

[**문제링크**](https://leetcode.com/problems/valid-palindrome/)
{% endhint %}

### 풀이

문제를 보면 `considering only alphanumeric and character and ignoring cases` 라는 조건이 있다.

따라서 인자로 받은 문자열 s 에 조작을 가해서 영어, 숫자로 만드는 작업이 필요함.

1. 정규표현식 `re` 모듈을 이용해서 원하지 않는 문자들을 없애자
2. 소문자로 만들자
3. 이후에는 `deque` 를 이용해서 앞과 뒤를 하나씩 `pop` 하면서 조사

```python
# re.sub(pattern, repl, string, count=0, flags=0)
s = re.sub([^A-Za-z0-9], "", s).lower()
```

```python
deq = collections.deque(s.lower())
while len(deq) > 1:
  if deq.popleft() != deq.pop():
    return False
  return True
```
