[백준(Baekjoon)][자바(java)] 11080 : Almost an Anagram

728x90

 

https://www.acmicpc.net/problem/11080

 

11080번: Almost an Anagram

Andy loves anagrams. For the uninitiated, an anagram is a word formed by rearranging the letters of another word, for example rasp can be rearranged to form spar. Andy is interested to know if two words are almost anagrams. A word is almost an anagram of a

www.acmicpc.net

 

문제 풀이

 

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());
	}
}

 

 

반응형