🚩 코딩테스트/알고리즘
[프로그래머스] [3차] 자동완성
딩딩크롱
2023. 2. 24. 23:22
728x90
문제
https://school.programmers.co.kr/learn/courses/30/lessons/17685
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
Trie 알고리즘을 사용해 풀었습니다.
코드
파이썬
def solution(words):
answer = 0
Trie = {}
for word in words:
cur_Trie = Trie
for x in word:
cur_Trie.setdefault(x, [0, {}])
cur_Trie[x][0] += 1
cur_Trie = cur_Trie[x][1]
for word in words:
cur_Trie = Trie
for i in range(len(word)):
if cur_Trie[word[i]][0] == 1:
break
cur_Trie = cur_Trie[word[i]][1]
answer += (i + 1)
return answer
728x90