728x90
https://www.acmicpc.net/problem/11080
문제 풀이
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static int anagram(String A, String B) {
int a[] = new int[26], d = 0;
for (char c : A.toCharArray())
a[c - 'a']++;
for (char c : B.toCharArray()) {
a[c - 'a']--;
if (a[c - 'a'] < 0) { d++; if (d > 1) return -1; }
}
return d;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
String s1 = st.nextToken(), s2 = st.nextToken();
br.close();
String A, B;
int l1 = s1.length(), l2 = s2.length();
if (l1 > l2) { A = s2; B = s1; }
else if (l1 < l2) { A = s1; B = s2; }
else { if (s1.compareTo(s2) > 0) { A = s2; B = s1; }
else { A = s1; B = s2; }
}
int d;
StringBuilder sb = new StringBuilder();
sb.append(A + " is ");
if (A.equals(B))
sb.append("identical to ");
else if ((d = anagram(A, B)) >= 0) {
if (d == 1) sb.append("almost ");
sb.append("an anagram of ");
}
else sb.append("nothing like ");
sb.append(B);
System.out.println(sb.toString());
}
}
반응형
'코딩 문제 풀기 ( Algorithm problem solving ) > 백준 온라인 저지 ( BOJ )' 카테고리의 다른 글
[백준(Baekjoon)][자바(java)] 6487 : 두 직선의 교차 여부 / 기하 (0) | 2021.11.27 |
---|---|
[백준(Baekjoon)][자바(java)] 11758 : CCW / 기하 (0) | 2021.11.27 |
[백준(Baekjoon)][자바(java)] 7587 : Anagram (0) | 2021.11.07 |
[백준(Baekjoon)][자바(java)] 1919 : 애너그램 만들기 (0) | 2021.11.06 |
[백준(Baekjoon)][자바(java)] 6996 : 애너그램 (0) | 2021.11.06 |