Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1157๋ฒˆ - ๋‹จ์–ด ๊ณต๋ถ€ (๊ตฌํ˜„, ๋ธŒ๋ก ์ฆˆ 1)

๊ฐ์ž ๐Ÿฅ” 2023. 2. 13. 15:11
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

https://www.acmicpc.net/problem/1157

 

1157๋ฒˆ: ๋‹จ์–ด ๊ณต๋ถ€

์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋œ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ด ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์ด ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋‚ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋‹จ, ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

๋ณด์ž๋งˆ์ž ์‹œ๊ฐ„์ดˆ๋ฅผ ์‚ดํˆ๋‹ค. ์ž…๋ ฅ๋ฐ›๋Š” ๋‹จ์–ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 100๋งŒ ์ด๊ณ , ์‹œ๊ฐ„์ดˆ... 2์ดˆ๋‹ค! 2์–ตํšŒ๊นŒ์ง€ ๋ฐ˜๋ณต์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ƒ๊ฐ๋ณด๋‹ค ์—ฌ์œ ๋กญ๊ฒŒ ์ฃผ์–ด์ง„ ๋ฌธ์ œ์˜€๋‹ค.

์—ฌ์œ ๋กญ๊ฒŒ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ํ•œ๋ฒˆ์”ฉ ๋ฐ˜๋ณต๋Œ๋ ค์•ผ๊ฒ ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๊ณ , ์ด์ œ ์–ด๋–ป๊ฒŒ ์ค‘๋ณตํšŸ์ˆ˜๋ฅผ ๊ฒ€ํ† ํ•ด์ค„๊ฑด์ง€ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ทธ๋ƒฅ ๋‚˜๋Š” ๋ณด์ž๋งˆ์ž ์•„์Šคํ‚ค์ฝ”๋“œ๋ฅผ ์ƒ๊ฐํ–ˆ๊ณ , ์•„์Šคํ‚ค์ฝ”๋“œ์— ๋งž์ถฐ์„œ 0๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ค€๋’ค ํ•ด๋‹น ์ž๋ฆฌ์— +1์”ฉ ํ•ด์ฃผ๊ธฐ๋Ÿฌํ–ˆ๋‹ค. 

๋‹จ์–ด๋Š” ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์—†์ด ์ฃผ์–ด์ง€๊ณ , ๋งˆ์ง€๋ง‰์—๋Š” ๋Œ€๋ฌธ์ž๋กœ ๊ฒฐ๊ตญ ์ถœ๋ ฅํ•ด์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋Œ€๋ฌธ์ž ๊ธฐ์ค€์œผ๋กœ ์•„์Šคํ‚ค์ฝ”๋“œ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค. ์•„์Šคํ‚ค์ฝ”๋“œ๋Š” ๋Œ€๋ฌธ์ž A๊ฐ€ 97, Z๊ฐ€ 122์ด๋ฏ€๋กœ, 0๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 122๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

ํ•œ ๊ธ€์ž์”ฉ -> ์•„์Šคํ‚ค์ฝ”๋“œ int๋กœ ๋ณ€ํ™˜ -> ํ•ด๋‹น ์ž๋ฆฌ์— +1 -> ๋งˆ์ง€๋ง‰์— max๊ฐ’์ฐพ์•„์„œ max๊ฐ’์ด 1๊ฐœ๋ผ๋ฉด ํ•ด๋‹น ์ž๋ฆฌ์˜ ๋Œ€๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์ถœ๋ ฅ

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

import Foundation

let word = readLine()!.map{ $0.uppercased() }

// A: 97 , Z: 122
private func solution(_ word: [String]) -> String {
    var alpahbet = Array(repeating: 0, count: 122)

    for char in word {
        alpahbet[Int(UnicodeScalar(char)!.value)] += 1
    }

    let maxNum = alpahbet.max()!
    if alpahbet.filter({ $0 == maxNum }).count > 1 {
        return "?"
    }
    return String(UnicodeScalar(alpahbet.firstIndex(of: maxNum)!)!)
}

print(solution(word))
๋ฐ˜์‘ํ˜•