
위 조건들을 만족한다면 YES, 만족하지 않는다면 NO를 출력하는 문제이다.
재배열해서 같게 하는 조건은 각 알파벳의 빈도수를 세어 같은지 판별했다.
다른 조건은 chatAt()메소드를 통해 어렵지 않게 구현할 수 있다.
N까지 도는 for문이 가장 크기 때문에 O(N)이 시간 복잡도이다.
해결언어 : Java
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Main {
static int N;
static String str1, str2;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
str1 = br.readLine();
str2 = br.readLine();
if (str1.charAt(0) != str2.charAt(0) || str1.charAt(N - 1) != str2.charAt(N - 1)) {
System.out.println("NO");
System.exit(0);
}
int[] freq1 = new int[26];
int[] freq2 = new int[26];
for (int i = 0; i < N; i++) {
freq1[str1.charAt(i) - 'a'] += 1;
freq2[str2.charAt(i) - 'a'] += 1;
}
for (int i = 0; i < 26; i++) {
if (freq1[i] != freq2[i]) {
System.out.println("NO");
System.exit(0);
}
}
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for (int i = 0; i < N; i++) {
char ch1 = str1.charAt(i);
if (!isVowel(ch1))
sb1.append(ch1);
char ch2 = str2.charAt(i);
if (!isVowel(ch2))
sb2.append(ch2);
}
if (sb1.toString().equals(sb2.toString()))
System.out.println("YES");
else
System.out.println("NO");
br.close();
}
static boolean isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
}
