We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public class HR080803Frequency {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
sc.close();
boolean ret = isAnagram(a, b);
System.out.println(ret ? "Anagrams" : "Not Anagrams");
}
public static boolean isAnagram(String a, String b) {
a = a.toLowerCase();
b = b.toLowerCase();
if (a.length() != b.length()) return false;
int[][] compare = new int[26][2]; // 26 chữ cái a-z
for (int i = 0; i < a.length(); i++) {
compare[a.charAt(i) - 'a'][0]++;
compare[b.charAt(i) - 'a'][1]++;
}
for (int i = 0; i < 26; i++) {
if (compare[i][0] != compare[i][1]) return false;
}
return true;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Anagrams
You are viewing a single comment's thread. Return to all comments →
public class HR080803Frequency { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.next(); String b = sc.next(); sc.close();