๋ฐฑ์ค€ - ๋‹จ์–ด๊ณต๋ถ€(Swift)

2022. 11. 8. 12:32ใ†Algorithm

๋ฐฑ์ค€ - ๋‹จ์–ด๊ณต๋ถ€(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

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

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

www.acmicpc.net

 

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

 

๋”•์…”๋„ˆ๋ฆฌ๋กœ ๊ฐฏ์ˆ˜๋ฅผ ์นด์šดํŠธํ•˜๋ฉด ๋˜๋Š” ์‰ฌ์šด ๊ตฌํ˜„๋ฌธ์ œ๋‹ค.

 

๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๋ฉด ํŠœํ”Œ ๋ฐฐ์—ด์ด ๋‚˜์˜ฌ ์ค„ ์•Œ์•˜๋Š”๋ฐ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ ๋‚˜์™”์—ˆ๋‹ค. (๋”•์…”๋„ˆ๋ฆฌ๋ฅผ mapํ•˜๋ฉด ๋ฐฐ์—ด์ด ๋‚˜์™€์„œ ๊ทธ๋ ‡๊ฒŒ ์ƒ๊ฐํ–ˆ๋‹ค)

 

import Foundation

var dic: [Character: Int] = [:]

let str = readLine()!.uppercased()
str.forEach{ x in 
    if dic[x] == nil { dic[x] = 1}
    else { dic[x]! += 1}
}
let maxVal = dic.values.max()
let arr = dic.filter{$0.value == maxVal}
if arr.count > 1 {
    print("?")
} else {
    print(String(arr.keys))
}