일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 영어원서읽기
- 읽기
- 미드시청
- 월간
- 운동
- Daily Challenge
- 화상영어
- 괜찮음
- English
- 영어공부
- realclass
- 파비최
- 매일
- leetcode
- 10분
- 스탭퍼
- 3줄정리
- 사이드
- 쓰릴오브파이트
- 개발자
- 잡생각
- Writing
- 30분
- 리얼 클래스
- Problem Solving
- 만화도
- 뭐든
- 프로젝트
- 링피트
- FIT XR
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/palindrome-pairs/
Palindrome Pairs - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Hard 난이도긴 한데.. n이 그렇게 크지 않아서 여러 번 loop 돌려서 처리 가능
class Solution:
def palindromePairs(self, words: List[str]) -> List[List[int]]:
backward, res = {}, []
for i, word in enumerate(words):
backward[word[::-1]] = i
for i, word in enumerate(words):
if word in backward and backward[word] != i:
res.append([i, backward[word]])
if word != "" and "" in backward and word == word[::-1]:
res.append([i, backward[""]])
res.append([backward[""], i])
for j in range(len(word)):
if word[j:] in backward and word[:j] == word[j-1::-1]:
res.append([backward[word[j:]], i])
if word[:j] in backward and word[j:] == word[:j-1:-1]:
res.append([i, backward[word[:j]]])
return res
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
Today's Challenge (0) | 2022.09.19 |
---|---|
Today's Challenge (0) | 2022.09.18 |
Today's Challenge (0) | 2022.09.16 |
Today's Challenge (0) | 2022.09.15 |
Today's Challenge (2) | 2022.09.14 |
Comments