MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / areAnagram

Method areAnagram

Programs/Anagram.java:12–43  ·  view source on GitHub ↗
(char str1[], char str2[])

Source from the content-addressed store, hash-verified

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[])

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected