| 4 | import java.util.*; |
| 5 | |
| 6 | class Anagram { |
| 7 | |
| 8 | static int NO_OF_CHARS = 256; |
| 9 | |
| 10 | /* function to check whether two strings |
| 11 | are anagram of each other */ |
| 12 | static boolean areAnagram(char str1[], char str2[]) |
| 13 | { |
| 14 | // Create 2 count arrays and initialize |
| 15 | // all values as 0 |
| 16 | int count1[] = new int[NO_OF_CHARS]; |
| 17 | Arrays.fill(count1, 0); |
| 18 | int count2[] = new int[NO_OF_CHARS]; |
| 19 | Arrays.fill(count2, 0); |
| 20 | int i; |
| 21 | |
| 22 | // For each character in input strings, |
| 23 | // increment count in the corresponding |
| 24 | // count array |
| 25 | for (i = 0; i < str1.length && i < str2.length; |
| 26 | i++) { |
| 27 | count1[str1[i]]++; |
| 28 | count2[str2[i]]++; |
| 29 | } |
| 30 | |
| 31 | // If both strings are of different length. |
| 32 | // Removing this condition will make the program |
| 33 | // fail for strings like "aaca" and "aca" |
| 34 | if (str1.length != str2.length) |
| 35 | return false; |
| 36 | |
| 37 | // Compare count arrays |
| 38 | for (i = 0; i < NO_OF_CHARS; i++) |
| 39 | if (count1[i] != count2[i]) |
| 40 | return false; |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | /* Driver code*/ |
| 46 | public static void main(String args[]) |
| 47 | { |
| 48 | char str1[] = ("gram").toCharArray(); |
| 49 | char str2[] = ("arm").toCharArray(); |
| 50 | |
| 51 | // Function call |
| 52 | if (areAnagram(str1, str2)) |
| 53 | System.out.println("The two strings are" |
| 54 | + " anagram of each other"); |
| 55 | else |
| 56 | System.out.println("The two strings are not" |
| 57 | + " anagram of each other"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 |
nothing calls this directly
no outgoing calls
no test coverage detected