ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Kotlin ๐Ÿฌ ๋ฐฑ์ค€ 6๋‹จ๊ณ„ :: 1157 ๋ฒˆ
    2023. 4. 28. 00:24
    ๋ฐ˜์‘ํ˜•

    ๋‹จ์–ด ๊ณต๋ถ€

    ๋ฌธ์ œ   |

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

     

    ์ž…๋ ฅ   |

      ์ฒซ์งธ ์ค„์— ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 1,000,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

     

    ์ถœ๋ ฅ   |

      ์ฒซ์งธ ์ค„์— ์ด ๋‹จ์–ด์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์„ ๋Œ€๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹จ, ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์ด ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ?๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    ํ’€์ด  |

      mutableMap์— ๋‹จ์–ด์˜ ๊ฐ character๋ฅผ key ๊ฐ’์œผ๋กœ ๋„ฃ๊ณ  value๋ฅผ ๊ธฐ๋ณธ 1๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

      ๋™์ผํ•œ key ๊ฐ’์ด ์žˆ์œผ๋ฉด value์— +1์„ ๋”ํ•ด์ฃผ๋ฉด์„œ ์ค‘๋ณต๋œ character์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ผ๋‹ค.

      ๋ชจ๋“  character๋ฅผ ์ฝ์–ด์˜ค๋ฉด ์ตœ์ข…์ ์œผ๋กœ max ๊ฐ’์„ ๊ฐ€์ง„ key์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด 1 ๊ฐœ๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด ?๋ฅผ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ทธ key ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    ๋‹ต์•ˆ  |

    fun main() {
        val word = readLine()!!.toString()
        val map = mutableMapOf<Char, Int>()
        
        word.forEach {
            val character = it.toLowerCase()
            
            if (map.containsKey(character)) {
                map[character] = map[character]!!.plus(1)
            } else {
                map.set(character, 1)
            }
        }
        
        val max = map.maxBy { it.value }
        if (map.filter { it.value == max!!.value }.count() > 1) {
            print("?")
        } else {
            print(max!!.key.toUpperCase())
        }
    }
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.