MCPcopy Index your code
hub / github.com/careercup/ctci / anagram

Method anagram

java/Chapter 1/Question1_3/Question.java:14–42  ·  view source on GitHub ↗
(String s, String t)

Source from the content-addressed store, hash-verified

12 }
13
14 public static boolean anagram(String s, String t) {
15 if (s.length() != t.length())
16 return false;
17 int[] letters = new int[128];
18 int num_unique_chars = 0;
19 int num_completed_t = 0;
20 char[] s_array = s.toCharArray();
21 for (char c : s_array) { // count number of each char in s.
22 if (letters[c] == 0)
23 ++num_unique_chars;
24 ++letters[c];
25 }
26 for (int i = 0; i < t.length(); ++i) {
27 int c = (int) t.charAt(i);
28 if (letters[c] == 0) { // Found more of char c in t than in s.
29 return false;
30 }
31 --letters[c];
32 if (letters[c] == 0) {
33 ++num_completed_t;
34 if (num_completed_t == num_unique_chars) {
35 // it�s a match if t has been processed completely
36 return true;
37 //return i == t.length() - 1;
38 }
39 }
40 }
41 return false;
42 }
43
44 public static void main(String[] args) {
45 String[][] pairs = {{"apple", "papel"}, {"carrot", "tarroc"}, {"hello", "llloh"}};

Callers 1

mainMethod · 0.95

Calls 1

lengthMethod · 0.45

Tested by

no test coverage detected