Publish:

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

๋ฌธ์ œ

๋ฌธ์ œ๋งํฌ

ํ’€์ด

์ด ๋ฌธ์ œ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ์— ์œ ์˜ํ•˜๋ฉด์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ํ•ด์•ผํ•œ๋‹ค. 2์ค‘ for ๋ฌธ์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ์— ๋Œ€๋ถ€๋ถ„ ์‹คํŒจํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

์ฒซ๋ฒˆ์งธ ์‹œ๋„..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        ...

    String s = scan.next();
    int length = s.length();
    int[] arr = new int[length];

    for (int i = 0; i < length; i++) {
      int count = 0;
      for (int j = 0; j < length; j++) {
        if (String.valueOf(s.charAt(i)).equalsIgnoreCase(String.valueOf(s.charAt(j)))) {
          arr[i] = ++count;
        }
      }
    }
          ...

n * n ์œผ๋กœ ์ „์ฒด ๋‹จ์–ด์—์„œ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž์—ด์˜ ๊ฐฏ์ˆ˜๋งŒํผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ ๋ฐฐ์—ด์— ์ €์žฅํ•˜๋‹ค๋ณด๋‹ˆ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๊ฐ€ ์ปค์ง€๋Š” ๊ฒฝ์šฐ์—” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค.

๋‘๋ฒˆ์งธ ์‹œ๋„..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  String s = scan.next().toUpperCase();
  int length = s.length();
  int[] arr = new int[length];

  for (int i = 0; i < length; i++) {
    char c = s.charAt(i);
    arr[i]++;
    for (int j = 0; j < i; j++) {
      if (c == s.charAt(j)) {
        arr[j]++;
        arr[i]--;
      }
    }
  }
        ...

๋‘๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์—์„œ ๋ฐ˜๋ณตํšŸ์ˆ˜๋ฅผ ์ค„์ด๊ณ  ์ฒ˜์Œ ๋ฌธ์ž์—ด์„ ๋ฐ›์„๋•Œ๋ถ€ํ„ฐ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ด ๋†“๊ณ  ํ’€์–ด๋„ ์‹คํŒจํ–ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋ ‡๊ฒŒ ํ’€๋‹ค๋ณด๋‹ˆ ๋‚˜์ค‘์— ์นด์šดํŠธ๊ฐ€ ์ €์žฅ๋œ ๋ฐฐ์—ด ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ์•ŒํŒŒ๋ฒณ ๊ธ€์ž๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋„ ์ƒ๊ฐ์„ ํ•ด์•ผํ–ˆ๋‹ค. ๊ฒฐ๊ตญ์—” 2์ค‘ for๋ฌธ ์ž์ฒด๊ฐ€ ๋ฌธ์ œ๊ฐ€ ๋˜๋Š”๊ฒƒ ๊ฐ™์•˜๋‹ค.

์†Œ์Šค ์ฝ”๋“œ

2์ค‘ for ๋ฌธ์ด ์‚ฌ์šฉ๋œ ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ–ˆ๋‹ค. ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๋น„๊ตํ•ด ๊ฐ€๋ฉด์„œ ์นด์šดํŠธ๋ฅผ ์…€ ํ•„์š”๊ฐ€ ์—†๋‹ค.

  1. 2์ค‘ for ๋ฌธ ์ œ๊ฑฐ
  2. ์ธ๋ฑ์Šค์™€ ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ ๋งคํ•‘

์ด ๋‘๊ฐ€์ง€๋ฅผ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์—์„œ ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ฐ ์•ŒํŒŒ๋ฒณ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณ ์œ  ascii ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฐฐ์—ด์— ์ €์žฅํ•ด๋†“๊ณ  ์ด๊ฑธ๋กœ ํ•˜๋‚˜์”ฉ ๋น„๊ตํ•œ๋‹ค.

1
  arr[c - 'A']++;

์ž…๋ ฅ๋˜๋Š” ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž ํ•˜๋‚˜์”ฉ ๋ฐฐ์—ด์— ์ €์žฅํ• ๋•Œ - 'A' ๋ฅผ ํ•˜๊ฒŒ๋˜๋ฉด ๋ฐฐ์—ด ์ธ๋ฑ์Šค์— ๋งž์ถฐ์„œ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด c ์— 'A' ๊ฐ€ ๋“ค์–ด์˜ค๋Š” ๊ฒฝ์šฐ 'A' - 'A' ๋Š” 0์ด ๋˜๊ณ  ๊ทธ๋Œ€๋กœ ๋ฐฐ์—ด ์ธ๋ฑ์Šค 0๋ฒˆ๋ถ€ํ„ฐ ์ €์žฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์ถ”ํ›„ ๋ฐฐ์—ด์—์„œ ์ธ๋ฑ์Šค๋กœ ๊บผ๋‚ธ๊ฒƒ์„ ๋‹ค์‹œ ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” ๊ณผ์ •๋„ ๊ฐ„ํŽธํ•˜๋‹ค.

1
  result = (char) ('A' + i);

๋Œ€๋ถ€๋ถ„์˜ 2์ค‘ for ๋ฌธ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ์— ์ƒ˜ํ”Œ์„ ๋ฏธ๋ฆฌ ์ค€๋น„ํ•ด๋†“๊ณ  ๋น„๊ตํ•ด๊ฐ€๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์•„๋ณด์ธ๋‹ค.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  ...

  String s = scan.next().toUpperCase();
  int length = s.length();
  int[] arr = new int[26];
  
  for (int i = 0; i < length; i++) {
    char c = s.charAt(i);
    arr[c - 'A']++;
  }
  
  int max = 0;
  char result = '?';
  for (int i = 0; i < arr.length; i++) {
    if (max < arr[i]) {
      max = arr[i];
      result = (char) ('A' + i);
    } else if (max == arr[i]) {
      result = '?';
    }
  }
  
  System.out.println(result);
๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ