๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐˜ผ๐™ก๐™œ๐™ค๐™ง๐™ž๐™ฉ๐™๐™ข/๐™‹๐™ง๐™ค๐™œ๐™ง๐™–๐™ข๐™ข๐™š๐™ง๐™จ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค '[PCCP ๋ชจ์˜๊ณ ์‚ฌ #1] 1๋ฒˆ - ์™ธํ†จ์ด ์•ŒํŒŒ๋ฒณ' ํŒŒ์ด์ฌ ํ’€์ด

by beomcoder 2024. 3. 15.
728x90
๋ฐ˜์‘ํ˜•

https://school.programmers.co.kr/learn/courses/20847/lessons/255900?language=python3

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

def solution(input_string):
    words = [input_string[0]]
    for word in input_string:
        if word != words[-1]:
            words.append(word)
    
    words = {answer:True for answer in words if words.count(answer) > 1}
    answer = ''.join(sorted(list(words.keys())))
    return answer if len(answer) else 'N'

 

๋ชจ์˜๊ณ ์‚ฌ๋ฅผ ํ’€์–ด๋ณด๋Š”๊ฑฐ๋ผ ํšจ์œจ์ด ์ข‹์€๊ฑธ ์ƒ๊ฐํ•˜๊ธฐ๋ณด๋‹จ ์ผ๋‹จ ๋นจ๋ฆฌ ํ’€์–ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ๋ง‰ ํ’€์–ด์„œ ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ์ง€์ €๋ถ„ํ•˜๊ณ  ์ดํ•ดํ•  ์ˆ˜ ์—†์„์ˆ˜๋„ ์žˆ๋‹ค.

words = [input_string[0]]
for word in input_string:
    if word != words[-1]:
        words.append(word)

'''
์„ค๋ช…๊ธ€์—์„œ ee, eee๋“ฑ์€ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ณ  e (๋‹ค๋ฅธ๋‹จ์–ด) e ์ด๋ ‡๊ฒŒ ์žˆ๋Š”๊ฒƒ๋งŒ ์ฐพ์•„์ฃผ๋ฉด ๋œ๋‹ค๊ณ  ํ•ด์„œ 
ee, eee๋“ฑ์„ ์ง€์›Œ์ฃผ๊ธฐ ์œ„ํ•ด ์œ„ ์ฝ”๋“œ๋ฅผ ์งฐ๋‹ค.
'''

 

words = {answer:True for answer in words if words.count(answer) > 1}
answer = ''.join(sorted(list(words.keys())))
return answer if len(answer) else 'N'

'''
๊ทธ๋ฆฌ๊ณ  ๋‹จ์–ด๋ฆฌ์ŠคํŠธ ์ค‘ ๊ฐ™์€ ๋‹จ์–ด๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ๊ฒƒ๋งŒ ๋ฝ‘์•„์ฃผ๊ธฐ ์œ„ํ•ด ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์“ด ์ด์œ ๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ๋‹จ์–ด๋ฅผ ๋Œ๋ฉด์„œ 2๊ฐœ ์ด์ƒ์ธ๊ฑธ ์ฐพ์œผ๋ฉด ์•ŒํŒŒ๋ฒณ์ด 2๊ฐœ ์ด์ƒ์”ฉ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์—
๊ทธ๊ฑธ set์œผ๋กœ ๋‹ค์‹œ ์“ฐ๊ธฐ ๊ท€์ฐฎ์•„์„œ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ผ๋‹ค.

๊ทธ๋‹ค์Œ ์•ŒํŒŒ๋ฒณ์ˆœ์„œ๋กœ ์ •๋ ฌ์„ ์‹œ์ผœ์ฃผ๊ณ  ์ •๋‹ต์ด ''์ด๋ฉด N, ์•„๋‹ˆ๋ฉด ์ •๋‹ต์„ ๋ฆฌํ„ดํ•ด์ฃผ์—ˆ๋‹ค.
'''

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€