티스토리 뷰
https://programmers.co.kr/learn/courses/30/lessons/43163
코딩테스트 연습 - 단어 변환
두 개의 단어 begin, target과 단어의 집합 words가 있습니다. 아래와 같은 규칙을 이용하여 begin에서 target으로 변환하는 가장 짧은 변환 과정을 찾으려고 합니다. 1. 한 번에 한 개의 알파벳만 바꿀 수
programmers.co.kr
def solution(begin, target, words):
if target not in words:
return 0
answer = 0
start = [begin]
while(len(words) != 0):
for s in start:
tmp = []
for word in words:
cnt = 0
for i in range(len(word)):
if s[i] != word[i]:
cnt += 1
if cnt == 2:
break
if cnt == 1:
tmp.append(word)
words.remove(word)
answer += 1
if target == "".join(tmp):
break
else:
start = tmp
return answer
'Algorithm > Programmers' 카테고리의 다른 글
[프로그래머스] 두 개 뽑아서 더하기 - Python (0) | 2020.09.26 |
---|---|
[프로그래머스] 같은 숫자는 싫어 - Python (0) | 2020.07.14 |
[프로그래머스] 위장 - Python (0) | 2020.07.12 |
[프로그래머스] 네트워크 - Python (0) | 2020.07.11 |
[프로그래머스] 모의고사 - Python (0) | 2020.07.11 |