카테고리 없음

[알고리즘 연습] 크기가 작은 부분 문자열, 가장 가까운 같은 글자

oogieon_n_on 2023. 6. 12. 21:34

https://school.programmers.co.kr/learn/courses/30/lessons/147355

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

def solution(t, p):
    out = len(t) - len(p) + 1 

    count = 0
    for i in range(out):
        sliced = t[i:i+len(p)]
        if int(sliced) <= int(p):
            count += 1
    return count

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/142086

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

def solution(s):
    answer = []
    last_index = {} # 순서대로 인덱스값을 가지는 dict 저장 
    for i in range(len(s)):
        if s[i] not in last_index: # 없을경우 -1 return 
            answer.append(-1)
        else:
            answer.append(i - last_index[s[i]]) # 거리 
        
        last_index[s[i]] = i  # loop을 돌며 자동적으로 가장 최근 index가 저장 
    
    return answer