forked from swapnanildutta/Hackerrank-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request swapnanildutta#132 from DiSousaDev/new-branch
Add Java Anagrams
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |