테스트 케이스는 통과했지만 채점을 통과하지 못함.
문제점1. 질문하기를 보고
"begin : hot
target : lot
words = {"hot", "dot", "dog", "lot", "log"}
hot -> lot 으로 1번만에 가능함에도 불구하고
hot -> dot -> lot으로 변환하고 있는것이 아닌지 살펴보세요"
라고 해서 확인해보니 2번 변환하는것으로 확인되었음.
from collections import deque
def solution(begin, target, words):
answer = 0
popword = ''
if target not in words:
return 0
else:
deq = deque()
deq.extend(words)
while deq:
popword = deq.popleft()
if (True if [c1 == c2 for c1, c2 in zip(begin, popword)].count(False) == 1 else False) and (True if [c1 == c2 for c1, c2 in zip(target, popword)].count(False) >= 2 else False):
answer += 1
begin = popword
elif (True if [c1 == c2 for c1, c2 in zip(begin, popword)].count(False) == 1 else False) and (True if [c1 == c2 for c1, c2 in zip(target, popword)].count(False) == 1 else False):
return answer + 2
❗해결: if 문을 사용해서 주어지 begin과 target이 한개만 차이날경우에는 return 1을 해주는 코드를 넣음.
begin: 처음 시작하는 단어
target: 만들어야하는 단어
words: 주어진 단어의 집합
answer: 최소 몇 단계의 과정을 거쳐 begin을 target으로 변환할 수 있는지 count하는 변수
popword: deq에서 꺼낸 단어를 담아두는 변수
from collections import deque
def solution(begin, target, words):
answer = 0
popword = ''
if target not in words:
return 0
elif (True if [c1 == c2 for c1, c2 in zip(begin, target)].count(False) == 1 else False):
return 1
else:
deq = deque()
deq.extend(words)
while deq:
popword = deq.popleft()
if (True if [c1 == c2 for c1, c2 in zip(begin, popword)].count(False) == 1 else False) and (True if [c1 == c2 for c1, c2 in zip(target, popword)].count(False) >= 2 else False):
answer += 1
begin = popword
elif (True if [c1 == c2 for c1, c2 in zip(begin, popword)].count(False) == 1 else False) and (True if [c1 == c2 for c1, c2 in zip(target, popword)].count(False) == 1 else False):
return answer + 2
🔗프로그래머스 - 단어 변환
https://programmers.co.kr/learn/courses/30/lessons/43163