๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

2020 KAKAO BLIND RECRUITMENT ๊ด„ํ˜ธ๋ณ€ํ™˜ Swift

by Fomagran ๐Ÿ’ป 2020. 4. 23.
728x90
๋ฐ˜์‘ํ˜•
  • ๊ด„ํ˜ธ ๋ณ€ํ™˜
  • darklight

    sublimevimemacs

    Swift 

๋ฌธ์ œ ์„ค๋ช…

์นด์นด์˜ค์— ์‹ ์ž… ๊ฐœ๋ฐœ์ž๋กœ ์ž…์‚ฌํ•œ ์ฝ˜์€ ์„ ๋ฐฐ ๊ฐœ๋ฐœ์ž๋กœ๋ถ€ํ„ฐ ๊ฐœ๋ฐœ์—ญ๋Ÿ‰ ๊ฐ•ํ™”๋ฅผ ์œ„ํ•ด ๋‹ค๋ฅธ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ž‘์„ฑํ•œ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ถ„์„ํ•˜์—ฌ ๋ฌธ์ œ์ ์„ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ˆ˜์ •ํ•˜๋ผ๋Š” ์—…๋ฌด ๊ณผ์ œ๋ฅผ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค. ์†Œ์Šค๋ฅผ ์ปดํŒŒ์ผํ•˜์—ฌ ๋กœ๊ทธ๋ฅผ ๋ณด๋‹ˆ ๋Œ€๋ถ€๋ถ„ ์†Œ์Šค ์ฝ”๋“œ ๋‚ด ์ž‘์„ฑ๋œ ๊ด„ํ˜ธ๊ฐ€ ๊ฐœ์ˆ˜๋Š” ๋งž์ง€๋งŒ ์ง์ด ๋งž์ง€ ์•Š์€ ํ˜•ํƒœ๋กœ ์ž‘์„ฑ๋˜์–ด ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
์ˆ˜์ •ํ•ด์•ผ ํ•  ์†Œ์Šค ํŒŒ์ผ์ด ๋„ˆ๋ฌด ๋งŽ์•„์„œ ๊ณ ๋ฏผํ•˜๋˜ ์ฝ˜์€ ์†Œ์Šค ์ฝ”๋“œ์— ์ž‘์„ฑ๋œ ๋ชจ๋“  ๊ด„ํ˜ธ๋ฅผ ๋ฝ‘์•„์„œ ์˜ฌ๋ฐ”๋ฅธ ์ˆœ์„œ๋Œ€๋กœ ๋ฐฐ์น˜๋œ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์„ ์•Œ๋ ค์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐœ๋ฐœํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์šฉ์–ด์˜ ์ •์˜

'(' ์™€ ')' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด ์žˆ์„ ๊ฒฝ์šฐ, '(' ์˜ ๊ฐœ์ˆ˜์™€ ')' ์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ์ด๋ฅผ ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์— '('์™€ ')'์˜ ๊ด„ํ˜ธ์˜ ์ง๋„ ๋ชจ๋‘ ๋งž์„ ๊ฒฝ์šฐ์—๋Š” ์ด๋ฅผ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, "(()))("์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด์€ ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด ์ด์ง€๋งŒ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์€ ์•„๋‹™๋‹ˆ๋‹ค.
๋ฐ˜๋ฉด์— "(())()"์™€ ๊ฐ™์€ ๋ฌธ์ž์—ด์€ ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด ์ด๋ฉด์„œ ๋™์‹œ์— ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด ์ž…๋‹ˆ๋‹ค.

'(' ์™€ ')' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด w๊ฐ€ ๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด ์ด๋ผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•ด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1. ์ž…๋ ฅ์ด ๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ, ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 2. ๋ฌธ์ž์—ด w๋ฅผ ๋‘ "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" u, v๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋‹จ, u๋Š” "๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"๋กœ ๋” ์ด์ƒ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์—†์–ด์•ผ ํ•˜๋ฉฐ, v๋Š” ๋นˆ ๋ฌธ์ž์—ด์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 3. ๋ฌธ์ž์—ด u๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด" ์ด๋ผ๋ฉด ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ๋‹ค์‹œ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 3-1. ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ u์— ์ด์–ด ๋ถ™์ธ ํ›„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 4. ๋ฌธ์ž์—ด u๊ฐ€ "์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด"์ด ์•„๋‹ˆ๋ผ๋ฉด ์•„๋ž˜ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. 4-1. ๋นˆ ๋ฌธ์ž์—ด์— ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž๋กœ '('๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค. 4-2. ๋ฌธ์ž์—ด v์— ๋Œ€ํ•ด 1๋‹จ๊ณ„๋ถ€ํ„ฐ ์žฌ๊ท€์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์„ ์ด์–ด ๋ถ™์ž…๋‹ˆ๋‹ค. 4-3. ')'๋ฅผ ๋‹ค์‹œ ๋ถ™์ž…๋‹ˆ๋‹ค. 4-4. u์˜ ์ฒซ ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ฌธ์ž์—ด์˜ ๊ด„ํ˜ธ ๋ฐฉํ–ฅ์„ ๋’ค์ง‘์–ด์„œ ๋’ค์— ๋ถ™์ž…๋‹ˆ๋‹ค. 4-5. ์ƒ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ท ํ˜•์žกํžŒ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด p๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ์ฃผ์–ด์ง„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ˆ˜ํ–‰ํ•ด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

๋งค๊ฐœ๋ณ€์ˆ˜ ์„ค๋ช…

  • p๋Š” '(' ์™€ ')' ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์ด๋ฉฐ ๊ธธ์ด๋Š” 2 ์ด์ƒ 1,000 ์ดํ•˜์ธ ์ง์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ๋ฌธ์ž์—ด p๋ฅผ ์ด๋ฃจ๋Š” '(' ์™€ ')' ์˜ ๊ฐœ์ˆ˜๋Š” ํ•ญ์ƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ๋งŒ์•ฝ p๊ฐ€ ์ด๋ฏธ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด ๊ทธ๋Œ€๋กœ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

p result
"(()())()" "(()())()"
")(" "()"
"()))((()" "()(())()"

ํ’€์ด

1.p๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด 2๋กœ ๋„˜์–ด๊ฐ€๊ณ  ๋น„์–ด์žˆ๋‹ค๋ฉด return "" ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

2.u๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์šฐ์„  var count๋กœ ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  p์•ˆ์˜ ๊ธ€์ž๋“ค์„ for๋ฌธ์„ ํ™œ์šฉํ•ด ๋งŒ์•ฝ "("๋ผ๋ฉด count์— +1์„ ํ•ด์ฃผ๊ณ 

")"๋ผ๋ฉด -1์„ ํ•ด์ค๋‹ˆ๋‹ค. ๊ท ํ˜•์žกํžŒ ๋ฌธ์ž์—ด์€ "("์™€ ")"์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๊ฐ™์€ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋Š” count๊ฐ€ 0์ด๋˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ 0์ด๋˜๋ฉด break๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค.

2.v๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” p์—์„œ u๋งŒํผ์„ ๋บ€ ๊ฒƒ์ด๋ฏ€๋กœ suffix๋ฅผ ํ™œ์šฉํ•˜์—ฌ p์˜ ๊ฐฏ์ˆ˜์—์„œ(p.count) u์˜ ๊ฐฏ์ˆ˜(u.count)๋ฅผ ๋นผ์ค๋‹ˆ๋‹ค.

3.u๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•˜๊ธฐ๋Š” ")"์˜ ๊ฐฏ์ˆ˜๊ฐ€ "("๋ณด๋‹ค ๋งŽ๋‹ค๋ฉด ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์ด๊ฑฐ ๋˜ํ•œ count๋ฅผ ํ™œ์šฉํ•˜์—ฌ count๊ฐ€ 0์•„๋ž˜๋กœ ๋‚ด๋ ค๊ฐ„๋‹ค๋ฉด ")" ๊ฐฏ์ˆ˜๊ฐ€ ๋” ๋งŽ์€ ๊ฒƒ์ด๋ฏ€๋กœ breakํ•ด์ค๋‹ˆ๋‹ค.

์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด 

3-1.u๋ฅผ ์•ž์— ๋ถ™์—ฌ์ฃผ๊ณ  solution(v)๋ฅผ ํ•ด์ฃผ๊ณ  ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด ์šฐ์„  u๋ฅผ ๋น„์–ด์žˆ๊ฒŒ ๋งŒ๋“ค๊ณ  for๋ฌธ์„ ํ™œ์šฉํ•ด u์— ์ฒซ๋ฒˆ์งธ์™€ ๋งˆ์ง€๋ง‰ ๊ธ€์ž๋นผ๊ณ  ")"๋ผ๋ฉด "("๋กœ "("๋ผ๋ฉด ")"๋กœ ๋ฐ”๊ฟ”์ค˜ ๋”ํ•ด์ค๋‹ˆ๋‹ค.

4.๊ทธ๋ฆฌ๊ณ  ๋ฐ˜ํ™˜์„ ๋‹ค์‹œ solution(v)๋ฅผ ํ•˜๊ณ  u๋ฅผ ๋”ํ•ด์ค๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ v๊ฐ€ ""๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

ex)()))(( 

u = (),v=))(( -> () + solution("))((")

u = ))((,v="" -> ( + solution("") + ) + ()

๋‹ต = ()()()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€ ์•„๋Š” ๋ฐฉ๋ฒ• "("์ˆ˜๋ณด๋‹ค ")"๊ฐ€ ๋” ๋งŽ์„ ๊ฒฝ์šฐ
//๊ท ํ˜•์žก์ธ ๋ฌธ์ž์—ด ์•„๋Š” ๋ฐฉ๋ฒ• -> "("์˜ ์ˆ˜์™€ ")"์˜ ์ˆ˜๊ฐ€ ๊ฐ™์œผ๋ฉด ๊ท ํ˜•์žกํžŒ ๊ฒƒ
import Foundation
func solution(_ p:String-> String{
    //1.๋นˆ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ๋ฉด
    if !p.isEmpty {
        var u = ""
        var count = 0
        
        //2 -> u๋ถ„๋ฆฌํ•˜๊ธฐ
        for char in p{
            if char == "(" {
                count += 1
                u += "("
            }else{
                count -= 1
                u += ")"
            }
            if count == 0 {
                break
            }
        }
        //2 -> v๋ถ„๋ฆฌํ•˜๊ธฐ
        let v = String(p.suffix(p.count-u.count))
        //3 u๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธํ•˜๊ธฐ
        for char in u{
            if char == "("{
                count += 1
            }else{
                count -= 1
            }
            if count < 0 {
                break
            }
        }
        let umap = u.map{String($0)}
        //3.์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋ผ๋ฉด
        if count < 0 {
            u = ""
            for i in 0..<umap.count{
                if i == 0 || i == umap.count - 1{
                 continue
                }
                else{
                    if umap[i] == "("{
                        u += ")"
                    }
                    else{
                        u += "("
                    }
                }
            }
            //4.๊ฒฐ๊ณผ์— u๋ถ™์—ฌ์„œ ๋ฐ˜ํ™˜
            return "(\(solution(v)))\(u)"
        }
        //3.์˜ฌ๋ฐ”๋ฅธ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด
        //3-1 u์— ๊ฒฐ๊ณผ๋ฅผ ๋ถ™์—ฌ์„œ ๋ฐ˜ํ™˜
        return "\(u)\(solution(v))"
    }
    //1.๋นˆ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด
    return ""
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

์ฒ˜์Œ ์ž˜๋ชป ์ดํ•ดํ•˜๊ณ  ํ‘ผ ๊ฒƒ

์ฒ˜์Œ์—” 1,2,3,4๊นŒ์ง€ ์ˆ˜ํ–‰ํ•˜๊ณ  4-1๋ถ€ํ„ฐ๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ ๋‚˜์˜จ u๋ฅผ ์ฒซ๋ฒˆ์งธ ๋งˆ์ง€๋ง‰์„ ์ œ๊ฑฐํ•˜๊ณ  ๋‹ค ๊ฑฐ๊พธ๋กœ ํ•ด์ฃผ๋Š” ์ค„ ์•Œ์•˜๋‹ค.

ex)()))(( 

u = (), v = ))((

answer = ()

u = ))((,v=""

answer = () + (())

๋‹ต = ()(()))

์ด๋ ‡๊ฒŒ ํ‘ธ๋Š” ๊ฑด ์ค„ ์•Œ์•˜๋‹ค. ๋‹ต๋„ ์˜ฌ๋ฐ”๋ฅด๊ณ  ๊ท ํ˜•์žกํžŒ ๊ฒƒ์œผ๋กœ ๋‚˜์˜ค๊ธธ๋ž˜ ์™œ ํ‹€๋ ธ๋Š”์ง€ ๋ชฐ๋ž๋Š”๋ฐ ๊ฒฐ๊ตญ ์žฌ๊ท€๋กœ ํ’€์–ด์•ผํ•˜๊ณ  ์‹œํ‚ค๋Š”๋ฐ๋กœ ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import Foundation
func solution(_ p:String-> String {
    var answer = String()
    var v = p
    while !v.isEmpty {
        var u = ""
        var count = 0
        for p in v{
            if p == "(" {
                count += 1
                u += "("
            }else{
                count -= 1
                u += ")"
            }
            if count == 0 {
                break
            }
        }
        let umap = u.map{String($0)}
        for i in u{
            if i == "("{
                count += 1
            }else{
                count -= 1
            }
            if count < 0 {
                break
            }
        }
        if count < 0 {
        for i in 0..<umap.count{
            if i == 0{
                answer += "("
            }
            else if i == umap.count-1{
                answer += ")"
            }
            else{
                if umap[i] == "("{
                    answer += ")"
                }
                else{
                    answer += "("
                }
            }
        }
        }else{
            answer += u
        }
        v = String(p.suffix(v.count-u.count))
    }
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€