-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_1157.java
More file actions
85 lines (70 loc) ยท 2.19 KB
/
_1157.java
File metadata and controls
85 lines (70 loc) ยท 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package backjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
// https://www.acmicpc.net/problem/1157
// ๋จ์ด ๊ณต๋ถ
public class _1157 {
public static void main(String[] args) throws IOException {
// sol1
// memory 26428 runtime 212
// word.length๋ ๋ณ์ํํ๊ณ alphabet.length๋ 26์ผ๋ก ๋ณ๊ฒฝํ๋ฉด memory 26616 runtime 220
/*
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] word = br.readLine().toUpperCase().toCharArray();
int[] alphabet = new int[26]; //์ํ๋ฒณ์ ์ด 26๊ฐ๋๊น 26๊ฐ์ ๋ฐฐ์ด ์ ์ธ
int maxCntOfalphabet = 0;
char ans = '?';
int overlap = 0;
// ์ฃผ์ด์ง ๋จ์ด ์ค ์ฌ์ฉ๋ ์ํ๋ฒณ์ ํ์๋ฅผ alphabet ๋ฐฐ์ด์ ๋ด๊ธฐ
for(int i=0; i<word.length; i++){
alphabet[(int)word[i] - 'A']++;
}
// ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋ ์ํ๋ฒณ ๊ตฌํ๊ธฐ
for(int i : alphabet){
maxCntOfalphabet = Math.max(maxCntOfalphabet, i);
}
// ์ถ๋ ฅํ๊ธฐ
for(int i=0; i<alphabet.length; i++){
if(alphabet[i] == maxCntOfalphabet){
ans = (char) (i+'A');
overlap++;
}
}
System.out.println( overlap > 1 ? "?" : ans);
*/
// sol2
// word.length()๋ฅผ ์์ฒด๋ฅผ for๋ฌธ์ ์ฌ์ฉ memory 20620 runtime 184
// word.length()๋ฅผ ๋ณ์ํํ ๋ค ์ฌ์ฉ memory 20416 runtime 192
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String word = br.readLine();
int[] alphabet = new int[26];
int len = word.length(); // length๋ฅผ ๋ณ์ํํ๋ฉด ๋ฉ๋ชจ๋ฆฌ 200์ ๋ ์ค๋๋ค๊ณ ๋ฐํ์์ 8์ด์ ๋ ๋์ด๋ ๋ค.
// ์ฃผ์ด์ง ๋จ์ด ์ค ์ฌ์ฉ๋ ์ํ๋ฒณ์ ํ์๋ฅผ alphabet ๋ฐฐ์ด์ ๋ด๊ธฐ
for(int i=0; i<len; i++) {
int idx = Character.toLowerCase(word.charAt(i)) - 'a';
alphabet[idx]++;
}
int max = -1;
char answer = '?';
for(int i=0; i<26; i++) {
if(alphabet[i] > max) {
max = alphabet[i];
answer = (char) (i+65);
} else if(alphabet[i] == max)
answer = '?';
}
System.out.println(answer);
}
}
/*
input
Mississipi
output
?
input
baaa
output
A
*/