๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

 

์‰ฝ๊ฒŒ ๋งํ•ด ํญํƒ„์ด ์žˆ๋Š” ๋ฌธ์ž๋ฅผ ์ง€์›Œ์ฃผ๋ฉด ๋œ๋‹ค. ๋˜ ์ง€์šฐ๊ณ  ํ•ฉ์นœ ์ƒํƒœ์—์„œ ํญํƒ„์ด ์žˆ์œผ๋ฉด ๋˜ ์ง€์›Œ์ฃผ๊ณ  ๋ฐ˜๋ณต

 

ํ’€์ด

์ด์ „์— ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์—์„œ ๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ ํ’€์–ด ๋ฐ”๋กœ ํ’€์ด๋ฒ•์„ ์ƒ๊ฐํ•ด ๋‚ผ ์ˆ˜ ์žˆ์—ˆ๋‹ค. (ํ–„๋ฒ„๊ฑฐ๋งŒ๋“ค๊ธฐ)

 

1. stack์— ๋ฌธ์ž์—ด ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด์ค€๋‹ค.

2. ๊ทธ๋•Œ๋งˆ๋‹ค stack์— ํญํƒ„์ด๋ž‘ ๋˜‘๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€ ์ฒดํฌํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ํญํƒ„์ˆ˜๋งŒํผ pop ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

์ฝ”๋“œ

let targetString = readLine()!.map { String($0) }
let bomb = readLine()!.map { String($0) }
var stack = [String]()

for i in 0..<targetString.count {
    stack.append(targetString[i])
    if stack.last! == bomb.last! // ํ˜„์žฌ ์Šคํƒ์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž์™€ ํญํƒ„์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž ๊ฐ™์€์ง€
        && stack.count >= bomb.count // ์Šคํƒ์˜ ๊ฐœ์ˆ˜๊ฐ€ ํญํƒ„์˜ ๊ฐœ์ˆ˜๋ณด๋‹ค ๋งŽ์€์ง€ (์ธ๋ฑ์Šค ์—๋Ÿฌ๋‚˜์ง€ ์•Š๊ฒŒ ์ฒดํฌ)
        && stack[stack.count-bomb.count..<stack.count].joined() == bomb.joined() { // ์Šคํƒ์— ํญํƒ„์ด ์žˆ๋Š”์ง€
        for _ in 0..<bomb.count {
            stack.removeLast()
        }
    }
}

print(stack.isEmpty ? "FRULA" : stack.joined())

 

ํ›„๊ธฐ

๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ ํ‘ผ ๊ฒฝํ—˜์ด ์žˆ์–ด ๋น„๊ต์  ์‰ฝ๊ฒŒ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

์•„์ง ๋งŽ์ด ๋ถ€์กฑํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€๋งŒ.. ์กฐ๊ธˆ์”ฉ ์„ฑ์žฅํ•ด๊ฐ„๋‹ค๊ณ  ๋Š๋‚€๋‹ค..!!

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...