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

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์‹œ์ € ์•”ํ˜ธ Swift

by Fomagran ๐Ÿ’ป 2020. 2. 23.
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์–ด๋–ค ๋ฌธ์žฅ์˜ ๊ฐ ์•ŒํŒŒ๋ฒณ์„ ์ผ์ •ํ•œ ๊ฑฐ๋ฆฌ๋งŒํผ ๋ฐ€์–ด์„œ ๋‹ค๋ฅธ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์•”ํ˜ธํ™” ๋ฐฉ์‹์„ ์‹œ์ € ์•”ํ˜ธ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด AB๋Š” 1๋งŒํผ ๋ฐ€๋ฉด BC๊ฐ€ ๋˜๊ณ , 3๋งŒํผ ๋ฐ€๋ฉด DE๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. z๋Š” 1๋งŒํผ ๋ฐ€๋ฉด a๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด s์™€ ๊ฑฐ๋ฆฌ n์„ ์ž…๋ ฅ๋ฐ›์•„ s๋ฅผ n๋งŒํผ ๋ฏผ ์•”ํ˜ธ๋ฌธ์„ ๋งŒ๋“œ๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด ๋ณด์„ธ์š”.

์ œํ•œ ์กฐ๊ฑด

  • ๊ณต๋ฐฑ์€ ์•„๋ฌด๋ฆฌ ๋ฐ€์–ด๋„ ๊ณต๋ฐฑ์ž…๋‹ˆ๋‹ค.
  • s๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ๋Œ€๋ฌธ์ž, ๊ณต๋ฐฑ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • s์˜ ๊ธธ์ด๋Š” 8000์ดํ•˜์ž…๋‹ˆ๋‹ค.
  • n์€ 1 ์ด์ƒ, 25์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

s n result
z 1 a
a B z 4 e F d
AB 1 BC

ํ’€์ด:์ฒ˜์Œ์— ์ƒ๊ฐํ–ˆ์„ ๋• ์•„์ฃผ ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

1.๊ณต๋ฐฑ์ธ์ง€ ์•„๋‹Œ์ง€ ๊ตฌ๋ณ„ํ•œ๋‹ค.

2.๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•œ๋‹ค.

3.ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์ด ๋ช‡ ๋ฒˆ์งธ์ธ์ง€ ๊ตฌํ•œ๋‹ค.

4.ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ์— n๋งŒํผ ๋”ํ•ด์ค€ ์ˆœ์„œ์— ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ์„ ๊ตฌํ•œ๋‹ค.

5.์•ŒํŒŒ๋ฒณ์ด 26๊ฐœ๋‹ˆ๊น 26์ด ๋„˜์–ด๊ฐ€๋ฉด -26์„ ํ•ด์ค€ ์•ŒํŒŒ๋ฒณ์„ ๊ตฌํ•œ๋‹ค.

์ด๋Ÿฐ ์‹์œผ๋กœ ๊ทธ๋ƒฅ ๋ชจ๋‘ for๋ฌธ์„ ์“ฐ๋‹ˆ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๋งˆ์ง€๋ง‰์—์„œ ์‹œ๊ฐ„์ดˆ๊ณผ๋ผ๊ณ  ๋–ด๋‹ค. ๋‚ด๊ฐ€ ์ƒ๊ฐํ•ด๋„ s์˜ ๊ธธ์ด๊ฐ€ 1000์ž๋งŒ ๋„˜์–ด๊ฐ€๋„ ์•„์ฃผ ๋น„ํšจ์œจ์ ์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ƒ๊ฐํ•œ ๊ฒŒ ๋‚ด๊ฐ€ ์ข‹์•„ํ•˜๋Š” ๋”•์…”๋„ˆ๋ฆฌ์˜€๋‹ค. ์•ŒํŒŒ๋ฒณ์„ key๊ฐ’์œผ๋กœ ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œ๋ฅผ value๊ฐ’์œผ๋กœ ๋„ฃ์–ด์คฌ๋‹ค. ๋˜ํ•œ key๊ฐ’๋งŒ ๋ชจ์•„์„œ ์ •๋ ฌํ•ด๋†“์€ ๋ฐฐ์—ด์œผ๋กœ keys๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

์šฐ์„  number๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์˜ ์ˆœ์„œ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์„ ์•Œ์•„๋‚ธ ๋’ค n๋งŒํผ ๋”ํ•ด์ฃผ๊ณ  

key๋“ค๋งŒ ๋ชจ์•„๋†“์€ keys์— ํ•ด๋‹น ์ˆซ์ž๋ฅผ ๋„ฃ์–ด์„œ n๋งŒํผ ๋”ํ•œ ์•ŒํŒŒ๋ฒณ์„ ์•Œ์•„๋‚ธ๋‹ค -> value

๋‹ค์‹œ ์•ŒํŒŒ๋ฒณ๋”•์…”๋„ˆ๋ฆฌ์— value๋ฅผ ๋„ฃ์–ด์ค˜์„œ ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์ด ๋ช‡ ๋ฒˆ์งธ์ธ์ง€ ์•Œ์•„๋‚ธ๋‹ค -> value1

๊ทธ๋ฆฌ๊ณ  ์•ŒํŒŒ๋ฒณ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค ์ค‘ value์˜ ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ์•ŒํŒŒ๋ฒณ์„ ์ฐจ๋ก€๋Œ€๋กœ ๋Œ€๋ฌธ์ž๋ผ๋ฉด ๊ทธ๋ƒฅ ๋”ํ•ด์ฃผ๊ณ 

์•„๋‹ˆ๋ฉด ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ˜•ํ•ด์„œ ๋„ฃ์–ด์ค€๋‹ค.

 

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func solution(_ s:String, _ n:Int-> String {
    var answer = String()
    let alphabet = ["A":0,"B":1,"C":2,"D":3,"E":4,"F":5,"G":6,"H":7,"I":8,"J":9,"K":10,"L":11,"M":12,"N":13,"O":14,"P":15,"Q":16,"R":17,"S":18,"T":19,"U":20,"V":21,"W":22,"X":23,"Y":24,"Z":25]
       let keys = alphabet.keys.sorted()
 
    for i in 0..<Array(s).count{
        if Array(s)[i] == " "{
            answer += " "
            continue
        }
            var number = alphabet[String(Array(s)[i].uppercased())]! + n
                                     if number > 25 {
                                     number = number - 26
                                              }
                                     let value = keys[number]
                                     let value1 = alphabet.index(forKey: value)
       if Array(s)[i].isUppercase{
            answer += alphabet.keys[value1!]
        }else {
            answer += alphabet.keys[value1!].lowercased()
        }
    }
    return answer
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด ์ค‘ ๊ฐ€์žฅ ์ข‹๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ

ํ’€์ด: ์šฐ์„  s๋ฅผ unicodeScalars์™€ map์„ ์ด์šฉํ•ด์„œ s์˜ ๋ฌธ์ž๋“ค์˜ unicedeScalars์˜ ๊ฐ’์„ Int๋กœ ํ˜•๋ณ€ํ™˜ํ•œ ๊ฐ’ ์ค‘ 65~90์€ ๋Œ€๋ฌธ์ž 97~122๋Š” ์†Œ๋ฌธ์ž 32๋Š” ๊ณต๋ฐฑ์ธ๋ฐ ๋Œ€๋ฌธ์ž,์†Œ๋ฌธ์ž์ธ ๊ฒฝ์šฐ ์•ŒํŒŒ๋ฒณ์˜ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆˆ๋‹ค์Œ ๋‹ค์‹œ ์ตœ์†Ÿ๊ฐ’์„ ๋”ํ•ด์ค˜ ํฌ๊ธฐ๋ฅผ ๋„˜์ง€ ์•Š๊ฒŒ ํ•ด์ฃผ๊ณ  ๊ณต๋ฐฑ์ธ ๊ฒฝ์šฐ๋Š” ๊ทธ๋ƒฅ ์ถœ๋ ฅํ•˜๊ฒŒ๋” ํ•ด์„œ opted๋ผ๋Š” ๋ฐฐ์—ด์— ๋‹ด๋Š”๋‹ค. ๊ทธ ๋ฐฐ์—ด์— ์žˆ๋Š” ์•„์Šคํ‚ค์ฝ”๋“œ ์ˆซ์ž๋“ค์„ ๋‹ค์‹œ Unicode.Scalar.init($0)๋กœ ํ•ด๋‹น ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ค€ ๋‹ค์Œ

flatMap์œผ๋กœ nil์ด ์•„๋‹Œ ๊ฒƒ๋“ค์„ ๋ชจ์•„์„œ (flatMap -> compactMap์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค.) ๋‹ค์‹œ ๋ฌธ์ž๋กœ ๋ณ€ํ˜•ํ•ด์ค€๋‹ค์Œ ํ•ฉ์ณ์ค€๋‹ค.

์‚ฌ์‹ค ์ดํ•ด๊ฐ€ ๋ฐฑํ”„๋กœ ๋˜์ง„ ์•Š์Œ.... ํ•˜์ง€๋งŒ

flatMap๊ณผ unicodeScalars๋ฅผ ์‚ฌ์šฉํ•œ ๊ฒƒ์— ๊ฐํƒ„..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func solution(_ s:String, _ n:Int-> String {
 
  let opted = s.unicodeScalars
    .map { unicodeScala -> Int in
      let number = Int(unicodeScala.value)
      switch number  {
      case 65...90return (number - 65 + n) % 26 + 65
      case 97...122return (number - 97 + n) % 26 + 97
      case 32return number
      default:
        print("NG")
        return number
      }
    }
    .map { Unicode.Scalar.init($0) }
    .flatMap { $0 }
    .map { Character.init($0) }
 
  return String(opted)
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€