동까의 코딩

99클럽 코테 스터디 2일차 TIL 본문

문제 풀이/99클럽

99클럽 코테 스터디 2일차 TIL

동까의 코딩 2025. 1. 14. 22:45
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/60060?language=python3

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

def solution(words, queries):
    answer = []
    for st in queries:
        cnt = 0
        start = 0
        end = len(st)
        
        if st[0] == '?':
                for j in range(len(st)):
                    if st[j] != '?':
                        start = j
                        break
        elif st[-1] == '?':
            for j in range(len(st) - 1, 0, -1):
                if st[j] != '?':
                    break
                else:
                    end -= 1
        cnt = 0
        if '?' not in st[start:end]:
            for i in range(len(words)):
                if len(st) != len(words[i]):
                    continue
                if st[start:end] == words[i][start:end]:
                    cnt += 1
        answer.append(cnt)
    return answer

생각나는데로 풀이를 해주니 테스트에서 3개가 틀려서 틀리게 되었고, 다양한 방법을 보던 중 아래의 풀이를 보게되었습니다.

 

힙을 사용한 풀이를 진행하였고, 조건에 대해 간결하게 처리해주셨습니다.

def solution(words, queries):
    head, head_rev = {}, {}
    wc = []
    
    def add(head,word):
        node = head
        for w in word:
            if w not in node:
                node[w]={}
            node= node[w]
            if 'len' not in node:
                node['len'] = [len_word]
            else:
                node['len'].append(len_word)
        node['end']=True   
    
    for word in words:
        len_word = len(word)
        add(head,word)
        add(head_rev,word[::-1])
        wc.append(len_word)
        
    def search(head, querie):
        count=0
        node = head
        for q in querie:
            if q=='?':
                return node['len'].count(len_qu)
            elif q not in node:
                break
            node = node[q]
        return count

    li=[]
    for querie in queries:
        len_qu = len(querie)
        if querie[0]=='?':
            if querie[-1]=='?': 
                li.append(wc.count(len_qu))
            else: 
                li.append(search(head_rev, querie[::-1]))
        else:
            li.append(search(head, querie))
    return li

출처 : https://velog.io/@hope1213/프로그래머스-가사검색-파이썬

 

[프로그래머스] 가사검색 (파이썬)

https://programmers.co.kr/learn/courses/30/lessons/60060본 문제는 정확성과 효율성 테스트 각각 점수가 있는 문제입니다.친구들로부터 천재 프로그래머로 불리는 "프로도"는 음악을 하는 친구로부터 자신이

velog.io

 

 

오늘은 이쯤으로 마무리하고 내일 다시 풀어보면서 새로운 풀이법을 익히도록 하겠습니다.

반응형

'문제 풀이 > 99클럽' 카테고리의 다른 글

99클럽 코테 스터디 4일차 TIL  (0) 2025.01.17
99클럽 코테 스터디 3일차 TIL  (0) 2025.01.16
99클럽 코테 스터디 1일차 TIL  (0) 2025.01.14
[99항해] 39일차 TIL  (0) 2024.06.28
[99클럽] 38일차 TIL  (0) 2024.06.28