일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 109. Convert Sorted List to Binary Search Tree
- 컴퓨터의 구조
- DWG
- LeetCode
- 시바견
- Generator
- 30. Substring with Concatenation of All Words
- attribute
- 715. Range Module
- Regular Expression
- Substring with Concatenation of All Words
- Class
- 밴픽
- Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- 프로그래머스
- shiba
- data science
- concurrency
- 315. Count of Smaller Numbers After Self
- Python Implementation
- t1
- 파이썬
- Decorator
- Python Code
- 운영체제
- iterator
- Protocol
- Python
- kaggle
Archives
- Today
- Total
Scribbling
프로그래머스: 단어 변환 본문
이와 유사한 문제를 이미 풀어보았다.
* for문 안에서 삭제할때는 항상 주의하자.
https://focalpoint.tistory.com/101
LeetCode: 126. Word Ladder II - 시바견의 끄적임
개인적으로 어려웠던 문제... A. 내가 푼 방법 일단 내가 푼 방법은 아래와 같다. 1) 모든 word에 대해 graph를 그린다. (word간 글자가 1개만 차이나는 경우 연결) 2) 다익스트라 알고리즘으로 최단 경
focalpoint.tistory.com
from collections import deque
def solution(begin, target, words):
if target not in words:
return 0
words = set(words)
words.discard(begin)
def neighbors(word):
for i in range(len(word)):
for c in 'abcdefghijklmnopqrstuvwxyz':
candidate = word[:i] + c + word[i+1:]
if candidate in words:
yield candidate
paths = deque()
paths.append([begin])
while paths:
path = paths.popleft()
if path[-1] == target:
return len(path) - 1
deletes = set()
for neighbor in neighbors(path[-1]):
paths.append(path+[neighbor])
deletes.add(neighbor)
words -= deletes
return 0
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 114. Flatten Binary Tree to Linked List (0) | 2021.11.02 |
---|---|
프로그래머스: 여행경로 (0) | 2021.11.01 |
프로그래머스: 네트워크 (0) | 2021.10.31 |
프로그래머스: 타겟 넘버 (0) | 2021.10.31 |
LeetCode: 115. Distinct Subsequences (0) | 2021.10.27 |