📌문제
https://school.programmers.co.kr/learn/courses/30/lessons/43163
🎖️난이도
Level 3
✔️풀이
# bfs..?
from collections import deque
def isPossible(word1, word2):
cnt = 0
for a1, a2 in zip(word1, word2):
if a1 != a2:
cnt += 1
return True if cnt == 1 else False
def solution(begin, target, words):
cnt = 0
queue = deque([[begin]]) # [["hit"], ["hot"], ["dot", "lot"], ["hot", "dog", "lot", ~~~], []]
if target not in words: return 0
while queue:
now_list = queue.popleft() # list
tmp = [] # 같은 레벨 있는 애들 싸그리 납치
for now in now_list:
if now == target:
return cnt
for word in words:
if isPossible(now, word):
tmp.append(word)
# 예외처리
if tmp:
queue.append(tmp)
cnt += 1
if cnt == len(words) + 1:
return 0
else:
return 0
return cnt
🧠노트
bfs인지 문자열 처리인지 구현인지...
🔍참고
-
'[ 알고리즘 ] > DFS, BFS' 카테고리의 다른 글
[백준] 14502.연구소 (0) | 2022.10.14 |
---|---|
[프로그래머스] 불량 사용자 (0) | 2022.09.09 |
[프로그래머스] 네트워크 (0) | 2022.09.09 |
[프로그래머스] 게임 맵 최단거리 (0) | 2022.09.03 |
[개념 정리] DFS, BFS (0) | 2022.07.12 |