TIL. 26 CodeKata#3

한상웅·2021년 7월 25일
0

CodeKata

목록 보기
2/4
post-thumbnail

Question

String 형인 str 인자에서 중복되지 않은 알파벳으로 이루어진 제일 긴 단어의 길이를 반환해주세요.

str: 텍스트
return: 중복되지 않은 알파벳 길이 (숫자 반환)
예를 들어,

str = "abcabcabc"
return 은 3
=> 'abc' 가 제일 길기 때문

str = "aaaaa"
return 은 1
=> 'a' 가 제일 길기 때문

str = "sttrg"
return 은 3
=> 'trg' 가 제일 길기 때문

풀이

def get_len_of_str(s):
    # 아래 코드를 작성해주세요.
    temp_list = []
    max_len = 0
    for num in s:
        if num in temp_list:
            temp_list = []
        temp_list.append(num)
        if len(temp_list) > max_len:
            max_len = len(temp_list)
    return max_len
print(get_len_of_str('a'))

max length를 0으로 지정 하고
1)temp list를 지정해서 for문을 이용해서 반복시키면서 temp list number하고 중복되면 제거 중복되지 않으면 붙여주는 방식.
2)만약 temp list의 길이가 max length보다 크면 templist 리턴하고
max length랑 templist 길이가 같으면 max length 리턴.

profile
Let's get it!

0개의 댓글