Program to check whether two strings are anagrams of each other I have written this code and I am wondering if the code could be optimised.Please review.

 public class Anagram1{
   public static boolean check(char[] c1,char[] c2,int l1,int l2){

     /*if the lengths of the strings are not equal,they cannot be anagrams of each other */

    if(l1!=l2){
        return false;
    }

    int count = 0;

    //nested for loops to compare the characters of both strings.
    //if the characters match,then the strings are anagrams of each other.
    for(int i=0; i&lt;l1; i++){
        for(int j=0; j&lt;l2; j++){
            if(c1[i]==c2[j]){
                count++;
                break;      //"break" used so as to count only a single occurrence of a
            }               //character.
        }
    }
    if(count==l1 &amp;&amp; count==l2){
        return true;
    }
    return false;
}

public static void main(String[] args) {

    String s1 = "abcd";

    String s2 = "acdb";

    char[] c1 = s1.toCharArray();

    char[] c2 = s2.toCharArray();

    int l1 = s1.length();

    int l2 = s2.length();

    System.out.println(check(c1,c2,l1,l2));
  }
}
About the algorithm (ITS NOT CORRECT). I will still review it for you:


Passing l1 and l2 in boolean check(char[] c1,char[] c2,int l1,int l2) as argument is redundant. You can use the inherent c1.length and c2.length provided by Java.
if(count==l1 &amp;&amp; count==l2) Any one check would do as you overruled l1!=l2 very early. Remember?
Failure cases:
"aab" and "abb" both would be called anagrams. Hope you discover that.


About the algorithm:


There are simpler better ways to check if two strings are anagram. How? Sort the strings and compare character by character.
Not impressed? Do a character count using a Map for each character and compare it for other array. And there are many more. Happy Googling.