Java Anagrams - HackerRank - New
Java Anagrams - HackerRank - New
Your Java Anagrams submission got 10.00 points. Share Tweet
You are now 97 points away from the gold level for your java badge.
Try the next challenge | Try a Random Challenge
Two strings, and , are called anagrams if they contain all the same characters in the same frequencies. For example, the anagrams of CAT are CAT, ACT, TAC,
TCA, ATC, and CTA.
Complete the function in the editor. If and are case-insensitive anagrams, print "Anagrams"; otherwise, print "Not Anagrams" instead.
Input Format
Constraints
Output Format
Print "Anagrams" if and are case-insensitive anagrams of each other; otherwise, print "Not Anagrams" instead.
Sample Input 0
anagram
margana
Sample Output 0
Anagrams
Explanation 0
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
Sample Input 1
anagramm
marganaa
Sample Output 1
https://www.hackerrank.com/challenges/java-anagrams/problem 1/3
9/9/2020 Java Anagrams | HackerRank
Not Anagrams
Explanation 1
The two strings don't contain the same number of a's and m's, so we print "Not Anagrams".
Sample Input 2
Hello
hello
Sample Output 2
Anagrams
Explanation 2
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
1 import java.util.Scanner;
2
3 public class Solution {
4
5 static boolean isAnagram(String str1, String str2) {
6 String a=str1.toLowerCase();
7 String b=str2.toLowerCase();
8 if (a.length() != b.length()) {
9 return false;
10 }
11 int count[] = new int[256];
12 for (int i = 0; i < a.length(); i++) {
13 count[a.charAt(i)]++;
14 count[b.charAt(i)]--;
15 }
16 for (int i = 0; i < 256; i++) {
17 if (count[i] != 0) {
18 return false;
19 }
20 }
21 return true;
22 }
23
24 public static void main(String[] args) { ⋯
Line: 5 Col: 54
Test against custom input
https://www.hackerrank.com/challenges/java-anagrams/problem 2/3
9/9/2020 Java Anagrams | HackerRank
3% 153/250
You solved this challenge. Would you Kudos on your progress! Take the HackerRank
Next Challenge Get Certified
like to challenge your friends? Skills Certification test and enrich your profile
Test case 0
Compiler Message
Success
Test case 1
2 margana
Test case 3
Test case 5
Test case 6
Contest Calendar | Blog | Scoring | Environment | FAQ | About Us | Support | Careers | Terms Of Service | Privacy Policy | Request a Feature
Could not connect to the reCAPTCHA service. Please check your internet connection and reload to get a reCAPTCHA challenge.
https://www.hackerrank.com/challenges/java-anagrams/problem 3/3