File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change
1
+ https://www.acmicpc.net/problem/1213
2
+ 팰린드롬이란 앞과 뒤에서 발음했을떄 모두 똑같은 발음을 할 수 있는 단어이다.
3
+
4
+ 팰린드롬이 되려면 홀수는 한개만 있어야한다.
5
+
6
+ string을 map으로 만들어 해당 단어의 개수를 세고 이를 정렬하여 하나씩 prefix suffix middle에 넣는 방식으로 진행 하였다.
7
+
8
+ 홀 수 일때만 middle에 넣으면 된다.
9
+
10
+ ```
11
+ package implemention;
12
+
13
+ import java.util.*;
14
+ import java.util.stream.Collectors;
15
+
16
+ public class b1213 {
17
+ public static void main(String[] args) {
18
+ //홀수가 더 많으면 안될듯
19
+
20
+ Scanner sc = new Scanner(System.in);
21
+ String[] str = sc.nextLine().split("");
22
+ Map<String, Integer> map = Arrays.stream(str)
23
+ .collect(Collectors.toMap(
24
+ word -> word,
25
+ word -> 1,
26
+ (a, b) -> a + b
27
+ ));
28
+ List<String> strings = new ArrayList<>(map.keySet());
29
+ Collections.sort(strings);
30
+
31
+ StringBuilder prefix = new StringBuilder();
32
+ StringBuilder suffix = new StringBuilder();
33
+ StringBuilder middle = new StringBuilder();
34
+
35
+ int odd = 0;
36
+ for (int i = 0; i < strings.size(); i++) {
37
+ int count = map.get(strings.get(i));
38
+ int value = count / 2;
39
+ if(count % 2 != 0) {//홀수라면
40
+ odd++;
41
+ middle.append(strings.get(i));
42
+ }
43
+ prefix.append(strings.get(i).repeat(value));
44
+ suffix.insert(0, strings.get(i).repeat(value));
45
+ }
46
+ if(odd > 1) {
47
+ System.out.println("I'm Sorry Hansoo");
48
+ } else {
49
+ System.out.println(prefix.toString() + middle.toString() + suffix.toString());
50
+ }
51
+ }
52
+ }
53
+ ```
You can’t perform that action at this time.
0 commit comments