DFS
from itertools import permutations as pmt
def solution(n):
#가능한 조합 만들기
mptlist = list(pmt(n,len(n)))
tmp =[]
#리스트로 변환
for i in mptlist:
word= ''.join(i)
tmp.append(word)
#중복 단어 제거
diclist = sorted(list(set(tmp)))
#다음 단어 반환 및 마지막 단어면 자신 반환
return diclist[diclist.index(n)+1] if n !=diclist[-1:] else n
print(solution("ABD"))
print(solution("ACBA"))
- 어려웠던 점 : 중복을 제거하는게 쉽지 않았다. 시험장에서는 딕셔너리를 이용해 중복을 없앴는데, 생각해보니 set 자료형을 다시 list로 만들면 인덱스 접근도 쉽게 할 수 있었던것 같다….. 어렵지는 않은 문제였지만, 복잡하게 풀었다.
- dfs를 할꺼면, 중간에 stop해서 permutaion 모듈보다 시간은 적지만, 여기서는 굳이 dfs할 필요 없이 permutation 해도 되는 문제 인것 같다.
Leave a comment