Skip to content

Instantly share code, notes, and snippets.

@ionox0
Created December 3, 2014 14:43
Show Gist options
  • Select an option

  • Save ionox0/e6bd08142298403614b6 to your computer and use it in GitHub Desktop.

Select an option

Save ionox0/e6bd08142298403614b6 to your computer and use it in GitHub Desktop.
Takes two arrays of words and checks if each of the words successive pair are anagrams of one another
class AnagramChecker {
public static void check_anagrams(String[] firstWords, String[] secondWords) {
String characters = "abcdefghijklmnopqrstuvwxyz";
int[] primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};
for (int i = 0; i < firstWords.length; i++){
char a;
char b;
int firstWordValue = 1;
int secondWordValue = 1;
for (int j = 0; j < firstWords[i].length(); j++){
if (j >= secondWords[i].length()) break;
a = firstWords[i].charAt(j);
b = secondWords[i].charAt(j);
firstWordValue *= primes[characters.indexOf(a)];
secondWordValue *= primes[characters.indexOf(b)];
}
if (firstWordValue == secondWordValue){
System.out.println(1);
} else {
System.out.println(0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment