Labels

algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Anagram Detector

/** Checks if two words are anagrams or not
*
@param firstWord
*
@param secondWord
*/
public static boolean areAnagram(String firstWord, String secondWord) {

int numberOfUnique = 0;
int processed = 0;
if (firstWord.length() != secondWord.length()) {
return false;
}
int[] chars = new int[256];
for (int i = 0; i < firstWord.length(); i++) {
if (chars[firstWord.charAt(i)] == 0) {
numberOfUnique
++;
}
chars[firstWord.charAt(i)]
++;
}
for (int i = 0; i < secondWord.length(); i++) {
// this char is not there so false
if (chars[secondWord.charAt(i)] == 0)
return false;

chars[secondWord.charAt(i)]
--;

if (chars[secondWord.charAt(i)] == 0) {
processed
++;
if (numberOfUnique == processed) {
System.out.println(processed);
return secondWord.length() == processed + 1;
}
}

}

5 comments:

  1. And here's the prototype that I knocked up in my workshop near Okehampton and found its way to Slapton via MozSpace London.Radar Detector Reviews

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. A metal detector finds the treasures that society has forgotten about or counted as lost. It can also find weapons that may have been buried deep in the ground.http://www.liedetector.uk

    ReplyDelete
  4. Please realize that I don't mean to recommend that each and every salon is doing this. But you have to be cautious in this matter. You can head to a tanning salon without the fear of being captured in the frame, if you truly play a little smart game.
    Lie Detector Test

    ReplyDelete
  5. I really like whenever you write. This kind of work is really praiseable thanks for sharing...http://www.liedetectortest.us

    ReplyDelete

Search 24 Bytes