0

Possible Duplicate:
Anagram algorithm in java

    public static boolean test(String a, String b) {
    a=a.toLowerCase();
    b=b.toLowerCase();
    boolean result = true ;
    boolean tmp1=false;

    if(a.length()==b.length()){
    for(int i=0;i<a.length();i++){
        tmp1=false;
        for(int k=0;k<b.length();k++){
            if(a.charAt(i)==b.charAt(k)){



                return true;
                }

        }
        if(tmp1==false){
            result=false;
            break;
        }
        if(i==a.length()-1)
            result=true;
        }
    }

    else {
        result=false;
        }



    return result;

}

I want to make a program to find anagram words.

The code works correctly when the input is

  • word one is dsa
  • second word is asd
  • The output is anagram (correct result)

The code fails for the input

  • first word is assa
  • second word is asaa
  • result is anagram (INCORRECT result)

What is my fault?

Cœur
  • 32,421
  • 21
  • 173
  • 232
  • 4
    Is this week anagram homework week? – durron597 Dec 04 '12 at 15:58
  • I would copy one of the many, many solutions already available. Or at least read them because there are much simpler and more efficient ways of doing this. – Peter Lawrey Dec 04 '12 at 15:59
  • 3
    @PeterLawrey: I would not. There is no learning in doing that. He's written code, determined it does not work, and is seeking to learn why. That is good. – Eric J. Dec 04 '12 at 15:59
  • @EricJ. He might learn that there are O(N ln N) solutions, with 6 lines of code, instead of O(N^2) but without some research the OP is unlikely to work that out for himself. – Peter Lawrey Dec 04 '12 at 16:00
  • go back to the definition of anagram. both strings need to have the same number of occurences of every characters. – UmNyobe Dec 04 '12 at 16:00
  • @durron597.. Add a worldwide before that. ;) – Rohit Jain Dec 04 '12 at 16:02

3 Answers3

4

Your algorithm decides that a word is an anagram too soon - in fact, as soon as it can match the first letter of the first word to any letter of the second word:

if(a.charAt(i)==b.charAt(k)){
    return true;
}

The easiest algorithm for anagram detection in Java is as follows:

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
1

You let i run from 0 to a.length() and k from 0 to b.length(). So both loop variables start at the beginning of the respective string.

Furthermore in the inner loop you immediately return true for the whole function, if at any point any character in b matches a character in a.

Sirko
  • 65,767
  • 19
  • 135
  • 167
0

First of all, if you're going to use statements such as return true or return false, use them consistently (get rid of boolean result).

The problem with this algorithm is that as soon as it detects a pair of identical characters, it will return true. To fix this, consider sorting the two strings and testing the equality of all characters (hint: Arrays.sort and String.toCharArray).

jma127
  • 1,422
  • 9
  • 13