[code_kata] 공통된 시작 단어 반환

*문제 😀


strs은 단어가 담긴 배열입니다.
공통된 시작 단어(prefix)를 반환해주세요.

예를 들어😉

strs = ['start', 'stair', 'step']
return'st'

strs = ['start', 'wework', 'today']
return''

풀이 😀

def get_prefix(strs):
    if len(strs) == 0:
        return ''

    empty = ''
    strs = sorted(strs)

    for i in strs[0]: 
        if strs[-1].startswith(empty+i): 
            empty += i 
        else:
            break
    return empty

strs = ['start', 'wework', 'today']
get_prefix(strs)

시각화 툴을 이용한 이해(pythontutor.com)