Created
February 4, 2013 15:51
-
-
Save SergeStinckwich/4707579 to your computer and use it in GitHub Desktop.
public static boolean isAnAnagram(String c1, String c2)
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
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