일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- iterator
- 파이썬
- 프로그래머스
- 30. Substring with Concatenation of All Words
- Decorator
- data science
- Convert Sorted List to Binary Search Tree
- t1
- 밴픽
- Generator
- concurrency
- Class
- Python
- LeetCode
- shiba
- Regular Expression
- 109. Convert Sorted List to Binary Search Tree
- 715. Range Module
- 315. Count of Smaller Numbers After Self
- Python Code
- 43. Multiply Strings
- 시바견
- kaggle
- 운영체제
- Python Implementation
- Protocol
- DWG
- Substring with Concatenation of All Words
- 컴퓨터의 구조
- attribute
Archives
- Today
- Total
Scribbling
LeetCode: 127. Word Ladder 본문
아래 문제와 거의 같다.
https://focalpoint.tistory.com/101
class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
wordSet = set(wordList)
wordSet.discard(beginWord)
if endWord not in wordSet:
return 0
def neighbor(word):
for i in range(len(word)):
for c in 'abcdefghijklmnopqrstuvwxyz':
candidate = word[:i] + c + word[i+1:]
if candidate in wordSet:
yield candidate
q = [[beginWord]]
while q:
temp_q = []
for i in range(len(q)):
last_word = q[i][-1]
if last_word == endWord:
return len(q[i])
deletes = []
for next_word in neighbor(last_word):
temp_q.append(q[i]+[next_word])
deletes.append(next_word)
wordSet -= set(deletes)
q = temp_q
return 0
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 128. Longest Consecutive Sequence (0) | 2021.11.08 |
---|---|
프로그래머스: 방의 개수 (0) | 2021.11.08 |
프로그래머스: 가장 먼 노드 (0) | 2021.11.05 |
LeetCode: 124. Binary Tree Maximum Path Sum (0) | 2021.11.04 |
LeetCode: 188. Best Time to Buy and Sell Stock IV (0) | 2021.11.04 |