반응형
answer = 0
def solution(begin, target, words):
dfs(begin,words,0,target)
print(answer)
return answer
def compare(word,target):
cnt = 0
for x,y in zip(word,target):
if x != y:
cnt +=1
if cnt == 1:
return True
def dfs(begin,words,cnt,target):
global answer
if target == begin: # 종료 조건 (1) : 같아진 경우
answer = cnt
return
else:
if len(words) == 0:
return
for idx in range(len(words)):
if compare(begin,words[idx]): # 재귀
word = words[:idx] + words[idx+1:]
dfs(words[idx],word,cnt+1,target) # 종료 조건에서 얻어냄
if __name__ == "__main__":
begin = "hit"; target ="cog"; words=["hot", "dot", "dog", "lot", "log", "cog"]
solution(begin,target,words)반응형
'IT 인터넷 > 프로그래머스' 카테고리의 다른 글
| 입국심사 (0) | 2023.01.09 |
|---|---|
| 모음사전 (0) | 2023.01.09 |
| (작성중)프로그래머스 (정렬) 가장 큰 수 - Python3 파이썬 (0) | 2022.03.11 |
| 프로그래머스 (정렬) K번째 수 - Python3 파이썬 (0) | 2022.03.11 |
| (작성중)프로그래머스 (해시) 베스트엘범 - Python3 (0) | 2022.03.10 |