๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 9046 - ๋ณตํ˜ธํ™”

by YWTechIT 2021. 5. 13.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 9046 - ๋ณตํ˜ธํ™”

๋ฌธ์ œ: ๋ฐฑ์ค€ 9046 - ๋ณตํ˜ธํ™”


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

์ €๋ฒˆ์— ํ’€์—ˆ๋˜ ๋‹จ์–ด ๊ณต๋ถ€์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ์ธ๋ฐ, ๋‹จ์ˆœํ•˜๊ฒŒ ์•ŒํŒŒ๋ฒณ์˜ ์‚ฌ์šฉ ๋นˆ๋„์ˆ˜๋ฅผ ์ฒดํฌํ•˜๋ฉด ๋˜๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ’€๋ฉด ๋œ๋‹ค. count, index ํ•จ์ˆ˜๋ฅผ ์•Œ๊ณ  ์žˆ์œผ๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

  1. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ์„ ์–ธํ•œ๋‹ค.
  2. ์ž…๋ ฅ ๋‚ด๋ถ€ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•œ๋‹ค(replace)
  3. ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž ๊ธธ์ด๋งŒํผ ๋นˆ ๋ฐฐ์—ด์„ ์„ ์–ธํ•œ๋‹ค.(๋นˆ๋„์ˆ˜ ์ฒดํฌ ์šฉ๋„)
  4. ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ord ์ˆซ์ž๋กœ ๋ฐ”๊พธ๊ณ  97์„ ๋นผ์ค€๋‹ค. (0๋ถ€ํ„ฐ ๋นˆ ๋ฐฐ์—ด์— ๋„ฃ๊ธฐ ์œ„ํ•ด)
  5. ๋งŒ์•ฝ, ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์˜ max๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด??๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
  6. ์•„๋‹ˆ๋ฉด, max๊ฐ’์˜ ์œ„์น˜๋ฅผ ์ฐพ๊ณ  ํ•ด๋‹น index์™€ 97์„ ๋”ํ•ด chrํ˜•์œผ๋กœ ๋ฐ”๊พผ๋‹ค. (4๋ฒˆ์—์„œ 97 ๋นผ์ค€ ๊ฒƒ์„ ์›๋ž˜๋Œ€๋กœ ๋”ํ–ˆ๋‹ค.)
T = int(input())

for _ in range(T):
    s = input().replace(' ', '')
    count_s = [0] * 26

    for i in s:
        count_s[ord(i) - 97] += 1

    if count_s.count(max(count_s)) > 1:
        print('?')
    else:
        print(chr(97 + count_s.index(max(count_s))))
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€