Code Kata(Python) week 1 - day 3

Jeongyun Heo·2021년 1월 27일
0
post-thumbnail

문제

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

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

예를 들어,

str = "abcabcabc"
return은 3
=> 'abc' 가 제일 길기 때문
str = "aaaaa"
return은 1
=> 'a' 가 제일 길기 때문
str = "aaaaa"
return은 1
=> 'a' 가 제일 길기 때문
str = "sttrg"
return은 3
=> 'trg' 가 제일 길기 때문

문제풀이 1

return 나오는 순간 for문 중간에 끝나니까 주의하기 ㅎㅎ
두 번째 for문이 실행이 안 되고 첫 번째 for문만 돌고 끝남

def get_len_of_str(s):
    result = []
    add = ""
    for i in s:
        if i not in add:
            add = add + i
        else:
            add = i
        result.append(add)

    a = 0
    for j in result:
        if len(j) > a:
            a = len(j)
    return a

문제풀이 2

def get_len_of_str(s):
    a = ''
    b = []
    c = 0
    for i in s:
        if i in a:
            a = ''
        a = a + i
        b.append(a)
    for i in b:
        if len(i) > c:
            c = len(i)
    return c

문제풀이 3

def get_len_of_str(s):
    count = 0
    ls = []
    for x in s:
      if x not in ls:
        ls.append(x)
      else:
        count = max(count, len(ls))
        ls = [x]
    return max(count, len(ls))

0개의 댓글