본문 바로가기

코딩 이야기/백준 풀이

백준 1342번: 행운의 문자열 파이썬코드(백트래킹)

반응형

https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/600px-Python-logo-notext.svg.png

s = input()
s = list(s)
su = len(s)
count = 0
alphabet = [0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0,0,0,0,0,
            0]

for i in range(len(s)):
    alphabet[ord(str(s[i])) - 97] += 1

def backtracking(l, p):
    global count
    if l == 0:
        count += 1
    for i in range(26):
        if alphabet[i] > 0 and i != p:
            alphabet[i] -= 1
            backtracking(l-1, i)
            alphabet[i] += 1
        else:
            pass

backtracking(su, -1)
print(count)

 

반응형