Skip to content

Commit

Permalink
Merge pull request swapnanildutta#132 from DiSousaDev/new-branch
Browse files Browse the repository at this point in the history
Add Java Anagrams
  • Loading branch information
swapnanildutta committed Oct 6, 2020
2 parents 95f9ebf + 2543272 commit 103f1d5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
72 changes: 72 additions & 0 deletions Java/Java_Anagrams/Anagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* 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
*/

package Java_Anagrams;

import java.util.Scanner;

public class Anagram {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}

static boolean isAnagram(String a, String b) {

// Declarations
int aLength = a.length();
int bLength = b.length();
int anagramLength = aLength < 25 ? aLength :25;
int[] anagram = new int[anagramLength];

// Check constraints
if (aLength < 1 || aLength > 50) return false;
if (bLength != aLength) return false;

// Convert strings to same case
a = a.toLowerCase();
b = b.toLowerCase();

// Increment / decrement counter for respective element
for (int i = 0; i < aLength; i++) {
anagram[(((int) a.charAt(i)) - 97) % aLength]++;
anagram[(((int) b.charAt(i)) - 97) % aLength]--;
}

// Search for counter not equal to 0
for (int i = 0; i < anagram.length; i++) {
if (anagram[i] != 0) return false;
}

return true;
}
}
1 change: 1 addition & 0 deletions Java/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Java
- [Java Comparator](https://www.hackerrank.com/challenges/java-comparator/problem)
- [Java Anagrams](https://www.hackerrank.com/challenges/java-anagrams/problem)

0 comments on commit 103f1d5

Please sign in to comment.