Anagram (Alan Smithee)

This commit is contained in:
PC de clase committed 2026-10-15 12:05:55 +02:00
1 parent 6c4a36dfef
commit 1b14dd3070
1 file changed
+42
+42
View File
@@ -0,0 +1,42 @@
import java.util.*;
public class P260_AlanSmithee {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Cuantos casos de prueba?");
int casos = sc.nextInt();
sc.nextLine();
for (int i = 0; i < casos; i++) {
String frase1 = sc.nextLine().toLowerCase();
String frase2 = sc.nextLine().toLowerCase();
if(isAnagram(frase1, frase2)) {
System.out.println("SI");
} else {
System.out.println("NO");
}
}
}
private static int vistos[] = new int[(int) ('z') - (int) ('a') + 1];
private static boolean isAnagram(String s1, String s2) {
for (int j = 0; j < s1.length(); j++){
char c = s1.charAt(j);
if(!Character.isLowerCase(c)) continue;
vistos[(int)(c) - (int)('a')]++;
}
for (int j = 0; j < s2.length(); j++){
char c = s2.charAt(j);
if(!Character.isLowerCase(c)) continue;
vistos[(int)(c) - (int)('a')]--;
}
for (int j = 0; j < vistos.length; j++){
if (vistos[j] != 0){
vistos[j] = 0;
return false;
}
}
return true;
}
}