Created
July 27, 2011 04:55
-
-
Save anupsavvy/1108710 to your computer and use it in GitHub Desktop.
Write a method to decide if two strings are anagrams are not.
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
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