[CodeKata] Week1 - Day3

ใ…Žใ…Žยท2021๋…„ 6์›” 23์ผ
0

algorithm ํ’€์ด

๋ชฉ๋ก ๋ณด๊ธฐ
37/44
post-custom-banner

๐Ÿ“Œ CodeKata - Week1, Day3

๋ฌธ์ œ

String ํ˜•์ธ str ์ธ์ž์—์„œ ์ค‘๋ณต๋˜์ง€ ์•Š์€ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ œ์ผ ๊ธด ๋‹จ์–ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ฃผ์„ธ์š”.

str: ํ…์ŠคํŠธ
return: ์ค‘๋ณต๋˜์ง€ ์•Š์€ ์•ŒํŒŒ๋ฒณ ๊ธธ์ด (์ˆซ์ž ๋ฐ˜ํ™˜)

์˜ˆ๋ฅผ ๋“ค์–ด,

str = "abcabcabc"
return ์€ 3
=> 'abc' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ
str = "aaaaa"
return ์€ 1
=> 'a' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ
str = "sttrg"
return ์€ 3
=> 'trg' ๊ฐ€ ์ œ์ผ ๊ธธ๊ธฐ ๋•Œ๋ฌธ

ํ’€์ด1

def get_len_of_str(s):
    # ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.
    arr = []
    max_len = 0
    
    for i in s:
      if i not in arr:
        arr.append(i)
      else:
        max_len = max(len(arr), max_len) 
        arr = []
        arr.append(i)
    
    max_len = max(len(arr), max_len) 
    return max_len

โžก๏ธ str = "sttrg"๋ฅผ ์˜ˆ๋ฅผ ๋“ค์–ด ์„ค๋ช…ํ•˜๋ฉด if๋ฌธ์—์„œ arr =[s,t]๊ฐ€ ๋“ค์–ด๊ฐ€๊ณ  , ์ด๋ฏธ arr ๋ฆฌ์ŠคํŠธ์— ๊ฐ™์€ t๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— else ๋ฌธ์œผ๋กœ ์ด๋™ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  max_len= 2 ๋กœ ๋ฐ”๋€Œ๊ณ , ๋‹ค์‹œ ๋ฆฌ์ŠคํŠธ arr์„ ์ดˆ๊ธฐํ™”ํ•ด์ค€ ๋‹ค์Œ์— ๋‹ค์‹œ t๋ถ€ํ„ฐ arr์— ๋„ฃ์–ด์ค€๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ๋‹ค์‹œ if๋ฌธ์œผ๋กœ ๋Œ์•„๊ฐ€ arr = [t,r,g]๋กœ ๋ฐ”๋€Œ๊ณ , for๋ฌธ์„ ๋‚˜์™€ max_len =3์œผ๋กœ ๋ฐ”๋€Œ๊ณ  return ํ•œ๋‹ค.

ํ’€์ด 2

post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€