seyeon1130 2025. 4. 8. 11:14

65. 문자열 나누기

def solution(s):
    answer=0
    d=[]
    b=0
    c=0
    if not s:
        return 0
    
    for i in range(len(s)):
        
        a = s[0]
        
        if s[i] ==a :
            b+=1
        else: c+=1
        
        if (b!=0) and (b==c):
            d = s[i+1:]
            answer+=1
            return answer + solution(d)
    return 1

66. 대충 만든 자판

 

예를 들어, 1번 키에 "A", "B", "C" 순서대로 문자가 할당되어 있다면 1번 키를 한 번 누르면 "A", 두 번 누르면 "B", 세 번 누르면 "C"가 되는 식입니다.

def solution(keymap, targets):
    answer = []
    tmp = {}
    num =0
    for i in keymap:
        for j in range(len(i)):
            if (i[j] not in tmp) or (tmp[i[j]]>j):
                tmp[i[j]] = j+1
    
    for h in targets:
        num=0
        for k in h:
            if k in tmp:
                num+=tmp[k]
            else:
                num = -1
                break
        answer.append(num)
            
    return answer

 

enumerate 쓰면 더 간단히 작성 가능

def solution(keymap, targets):
    answer = []
    tmp = {}
    num =0
    for i in keymap:
        for ke, val in enumerate(i):
            if (val not in tmp) or (tmp[val]>ke):
                tmp[val] = ke+1
    
    for h in targets:
        num=0
        for k in h:
            if k in tmp:
                num+=tmp[k]
            else:
                num = -1
                break
        answer.append(num)
 

69. 둘만의 암호

예를 들어 s = "aukks", skip = "wbqd", index = 5일 때, a에서 5만큼 뒤에 있는 알파벳은 f지만 [b, c, d, e, f]에서 'b'와 'd'는 skip에 포함되므로 세지 않습니다. 따라서 'b', 'd'를 제외하고 'a'에서 5만큼 뒤에 있는 알파벳은 [c, e, f, g, h] 순서에 의해 'h'가 됩니다. 나머지 "ukks" 또한 위 규칙대로 바꾸면 "appy"가 되며 결과는 "happy"가 됩니다.

 

def solution(s, skip, index):
    answer = ''
    for i in s:
        tmp =''
        move =0
        ch = ord(i)
        while move<index:
            ch+=1
            
            if ch >ord('z'):
                ch=ord('a')
                
            if chr(ch) in skip:
                continue
            move+=1
        tmp = chr(ch)
        answer+=tmp
        
    return answer