0% found this document useful (0 votes)
245 views

Java Anagrams - HackerRank - New

This document describes a Java Anagrams problem on HackerRank. The problem asks the user to write a function that checks if two strings are anagrams, which are strings that contain the same characters with the same frequency. The function should return "Anagrams" if they are anagrams, and "Not Anagrams" otherwise. Sample inputs and outputs are provided to demonstrate cases where strings are and aren't anagrams when considering character frequencies in a case-insensitive way.

Uploaded by

Nikhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
245 views

Java Anagrams - HackerRank - New

This document describes a Java Anagrams problem on HackerRank. The problem asks the user to write a function that checks if two strings are anagrams, which are strings that contain the same characters with the same frequency. The function should return "Anagrams" if they are anagrams, and "Not Anagrams" otherwise. Sample inputs and outputs are provided to demonstrate cases where strings are and aren't anagrams when considering character frequencies in a case-insensitive way.

Uploaded by

Nikhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/9/2020 Java Anagrams | HackerRank

 

97 more points to get your gold badge!


Java Anagrams  Rank: 147353 | Points: 153/250
Java


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

Problem Submissions Leaderboard Editorial 

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

The first line contains a string denoting .


The second line contains a string denoting .

Constraints

Strings and consist of English alphabetic characters.

The comparison should NOT be case sensitive.

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

Character Frequency: anagram Frequency: margana


A or a 3 3
G or g 1 1
N or n 1 1
M or m 1 1
R or r 1 1

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

Character Frequency: anagramm Frequency: marganaa


A or a 3 4
G or g 1 1
N or n 1 1
M or m 2 1
R or r 1 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

Character Frequency: Hello Frequency: hello


E or e 1 1
H or h 1 1
L or l 2 2
O or o 1 1

The two strings contain all the same letters in the same frequencies, so we print "Anagrams".

Change Theme Java 8

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

Upload Code as File Run Code Submit Code


You have earned 10.00 points!


You are now 97 points away from the gold level for your java badge.

3% 153/250

Congratulations Earn a certificate in Java

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

Input (stdin) Download


Test case 2
1 anagram

2 margana
Test case 3

Expected Output Download


Test case 4
1 Anagrams

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

You might also like