### Want a Free Job Posting?

Buy a job posting today and the second one is on us. For a limited time only. Act Now.

Amazon.com

Engaged Employer

Interview

# Find if a word is anagram (e.g. ana is an anagram).

1

Hash each character check if sum equal

Anonymous on Apr 21, 2014
1

public static boolean isAnagram(String wordA, String wordB) { if(wordA.length() != wordB.length()) return false; int sumA = 0, sumB = 0; for(int i=0; i &lt; wordA.length(); sumA += wordA.charAt(i), sumB += wordB.charAt(i), i++); return sumA == sumB; }

Matteo Gobbi on Jun 15, 2014
0

Note that I abhor multiple return statements, but I did it here to save space. // O(n/2) -&gt; O(n) public static boolean verify(String str) { int length = str.length(); char [] strArray = str.toCharArray(); int mid1 = length / 2; int mid2 = mid1; if ( length % 2 &gt; 0 ) { mid2++; } for( int i = 0, j = length - 1; i &lt;= mid2 && j &gt;= mid1; i++,j-- ) { if ( strArray[i] != strArray[j] ) { return false; } } return true; }

Sean Roy on Aug 5, 2014
1

public static boolean isAnagram(String s1, String s2) { s1 = s1.toLowerCase().replaceAll("[^a-z]", ""); s2 = s2.toLowerCase().replaceAll("[^a-z]", ""); char[] c1 = s1.toCharArray(); Arrays.sort(c1); char[] c2 = s2.toCharArray(); Arrays.sort(c2); return Arrays.equals(c1, c2); }

Hieu Phan on Aug 7, 2014
0

More detailed answer: http://hackergym.com/#/problems/d484fb87-cd9c-48aa-a013-84c21dfc56f2 It's problem 4 on that page.

Kevin on Aug 26, 2014