[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ์ง์ง€์–ด ์ œ๊ฑฐํ•˜๊ธฐ

 

1. ๋ฌธ์ œ : Link

์—ฐ์†๋˜๋Š” ๋ฌธ์ž๋ฅผ ์ฐพ์•„ ์ œ๊ฑฐํ•ด์ฃผ๋Š” ๋ฌธ์ œ

 

2. ํ’€์ด

์Šคํƒ์œผ๋กœ ํ’€์–ด์•ผํ•˜๋Š” ๊ฑด ์•Œ์•˜์ง€๋งŒ ๋นˆ ๋ฆฌ์ŠคํŠธ๊นŒ์ง€ ์ƒ๊ฐํ•ด๋†“๊ณ  ๊ฑฐ๊ธฐ์— appendํ•˜๋Š” ๋ฐฉ์‹์„ ๋„ˆ๋ฌด ์ด์ƒํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ๋‹ค.

๊ธฐ๋ณธ์— ์ถฉ์‹คํ•œ ์ฝ”๋“œ๋ฅผ ์งœ๋„๋ก ํ•˜์ž.

 

๊ตณ์ด ์žฌ๊ท€๋กœ ๋Œ๋ฆด ํ•„์š” ์—†์—ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค.

 

3. ์ฝ”๋“œ

1)

def check1(p):
    tmp = []
    origin = p.copy()

    while p:
        if len(p) == 1:
            tmp.append(p.pop())
            break
        first = p.pop()
        second = p.pop()
        if first != second:
            tmp.append(first)
            p.append(second)

    if not tmp:
        return 1
    tmp.reverse()
    if origin == tmp:
        return 0
    return check1(tmp)
    
        
def solution(s):
    ss = list(s)
    result = check1(ss)
    return result

2)

def solution(s):
    stack = []
    for i in s:
        if len(stack) == 0: 
            stack.append(i)
        elif stack[-1] == i: 
            stack.pop()
        else: 
            stack.append(i)
    if len(stack) == 0: 
        return 1
    else: 
        return 0

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ