Skip to content

Instantly share code, notes, and snippets.

@anupsavvy
Created July 27, 2011 04:55
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 anupsavvy/1108710 to your computer and use it in GitHub Desktop.
Save anupsavvy/1108710 to your computer and use it in GitHub Desktop.
Write a method to decide if two strings are anagrams are not.
public class Puzzle{
public static boolean findAnagrams(String s1, String s2){
if(s1 == null && s2!=null)
return false;
if(s1!=null && s2==null)
return false;
if(s1==null && s2==null)
return true;
if(s1.length()!=s2.length())
return false;
int[] letters = new int[256]; // considering ascii character set.
for(int i =0;i<256;i++){
letters[i]=0;
}
for(int i =0; i < s1.length(); i++){
letters[s1.charAt(i)]++;
}
for(int i=0; i < s2.length(); i++){
if(letters[s2.charAt(i)] == 0)
return false;
else{
letters[s2.charAt(i)]--;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment