Given a word, find other words in an array with same length and same characters I tried solving the problem in the following manner; I am just a beginner and wanted to know my mistakes and a more efficient way with better time complexity (if any).

    public class d3 {
    public static void main(String[] args){
    String Search="loop";
    String[] letters = Search.split("") ;
    int counter;

    String[] words={"pole","pool","lopo","book","kobo"};

    for(int i=0;i&lt;words.length;i++)
    {
        counter=0;
        String ssplit[] = words[i].split("");
        for(int j=0;j&lt;words[i].length();j++)
        {

            if(letters.length==ssplit.length)
            {
            for(int k=0;k&lt;letters.length;k++)
            {
                    if(letters[j].equals(ssplit[k]))
                        {counter++;
                        ssplit[k]="*";
                break;


                        }



            }
            if (counter == 4)
            {
                System.out.println(words[i]);

            }

        }
            }


    }

}
}
Whitespace is very important for readability. For posting to Stack Exchange sites I recommend replacing tabs with spaces, because otherwise the site does that for you and the tabstops might not match. Here, though, the whitespace is so crazy that I think you need to look at configuring your IDE to pretty-print the code. Reformatting so that I can understand the structure:


public class d3 {
    public static void main(String[] args){
        String Search="loop";
        String[] letters = Search.split("") ;
        int counter;

        String[] words={"pole","pool","lopo","book","kobo"};

        for(int i=0;i&lt;words.length;i++)
        {
            counter=0;
            String ssplit[] = words[i].split("");
            for(int j=0;j&lt;words[i].length();j++)
            {
                if(letters.length==ssplit.length)
                {
                    for(int k=0;k&lt;letters.length;k++)
                    {
                        if(letters[j].equals(ssplit[k]))
                        {
                            counter++;
                            ssplit[k]="*";
                            break;
                        }
                    }
                    if (counter == 4)
                    {
                        System.out.println(words[i]);
                    }
                }
            }
        }
    }
}





Names

Java convention is that camel-case names which start with a capital letter are types (classes, interfaces, etc), so Search as the name of a variable is unexpected.

counter is not entirely uninformative, but a better name would tell me what it counts. Similarly, it would be helpful to distinguish which variables relate to the search query and which to the items searched. The best convention I've seen there is PHP's needle and haystack, so I would suggest needleLetters and haystackWords.



foreach statement

Instead of for(int i=0;i&lt;words.length;i++) ... words[i] you can use for (String word : words) ... word. This removes a variable and simplifies the naming, making it easier to see what the code does.



Decomposing strings

String has a method toCharArray(). I think it would make more sense to use that than split("").



Don't put something in a loop which can go outside it


            for(int j=0;j&lt;words[i].length();j++)
            {
                if(letters.length==ssplit.length)
                {
                    ...
                }
            }



could be rewritten

            if(letters.length==ssplit.length)
            {
                for(int j=0;j&lt;ssplit.length;j++)
                {
                    ...
                }
            }


Executing the test once is more efficient, and it's also easier to understand because the maintainer doesn't have to reason about loop invariants to work out what might have changed the second time the test is executed.

Since there's nothing after this test in the loop body, an alternative would be

            if(letters.length!=ssplit.length)
            {
                continue;
            }

            for(int j=0;j&lt;ssplit.length;j++)
            {
                ...
            }




Beware hard-coded constants

Why

                    if (counter == 4)
                    {
                        System.out.println(words[i]);
                    }


? That's a bug. The comparison should be with letters.length. Also, it would make more sense to move the test outside the loop over j.



Use advanced data structures


            for(int j=0;j&lt;words[i].length();j++)
            {
                for(int k=0;k&lt;letters.length;k++)
                {
                    if(letters[j].equals(ssplit[k]))
                    {
                        counter++;
                        ssplit[k]="*";
                        break;
                    }
                }
            }



takes time proportional to words[i].length() * letters.length. If you use java.util.HashMap&lt;Character, Integer&gt; to store a per-character count, you can generate a representation for each word in time proportional to the length of the word, and you can compare the representations of two words in time proportional to the length of each word. In this toy example it doesn't matter, but for real applications the difference between \$O(n^2)\$ and \$O(n)\$ can be the difference between a project being feasible and not. The first place to look for optimisations is always the algorithm.