Skip to content

Instantly share code, notes, and snippets.

@SergeStinckwich
Created February 4, 2013 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergeStinckwich/4707579 to your computer and use it in GitHub Desktop.
Save SergeStinckwich/4707579 to your computer and use it in GitHub Desktop.
public static boolean isAnAnagram(String c1, String c2)
class Exo3 {
public static boolean isAnAnagram(String c1, String c2){
int [] counts = new int[26];
int l1 = c1.length();
int l2 = c2.length();
if (c1.equals(c2)) return true;
if (l1 != l2) return false;
for (int i=0; i<l1; i++)
counts[(int)(c1.charAt(i))-97]++;
for (int i=0; i<l1; i++)
counts[(int)(c2.charAt(i))-97]--;
for (int i=0; i<l1; i++)
if (counts[i] != 0) return false;
return true;
}
public static void main(String args[]){
System.out.println(isAnAnagram("", "")== true);
System.out.println(isAnAnagram("x", "x") == true);
System.out.println(isAnAnagram("rage", "gare")==true);
System.out.println(isAnAnagram("hello", "world") == false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment