| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 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 | 
													Tags
													
											
												
												- 30분
 - English
 - 운동
 - 영어공부
 - 매일
 - FIT XR
 - 스탭퍼
 - 쓰릴오브파이트
 - 10분
 - 개발자
 - Daily Challenge
 - Writing
 - 뭐든
 - 잡생각
 - leetcode
 - 영어원서읽기
 - Problem Solving
 - 리얼 클래스
 - 링피트
 - 프로젝트
 - 괜찮음
 - 화상영어
 - 읽기
 - 사이드
 - 미드시청
 - 만화도
 - 3줄정리
 - 파비최
 - 월간
 - realclass
 
													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