Checking if all possible combinations of any two array elements in an array contains 0-9 atleast once I am coding for an online platform. The code is working on some test cases but is getting a Time limit exceeded. What are the ways to make my code more efficient?


  Input

5 
129300455
5559948277 
012334556
56789 
123456879

  
  Output

5

  
  Explanation
  
  Winning Pairs:
  
  129300455 56789 -->contains all from 0-9 atleast once
  
  129300455 123456879-->contains all from 0-9 at least once etc


   import java.io.*;
   import java.util.*;
   import java.text.*;
   import java.math.*;
   import java.util.regex.*;

   public class Main {

static int winningLotteryTicket(String[] tickets,int n)
 // tickets = array of all strings

 {
    int count=0;
    boolean b=false;
    // the i and j loop are to create all possible combinations
    for(int i=0;i&lt;n;i++)
{

    for(int j=i+1;j&lt;n;j++)
    {

     if(i!=j)
     { String check=tickets[i]+tickets[j];

     // running loop to check from 0-9 atleast once
     for(int u=0;u&lt;=9;u++)
     {
         String r=u+"";
         b=check.contains(r);
         if(!b)
         {
         break;
         }

     }
     if(b){count++;}       
     }
    }
}
    return count;
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    String[] tickets = new String[n];
    for(int tickets_i = 0; tickets_i &lt; n; tickets_i++){
        tickets[tickets_i] = in.next();
    }
    int result = winningLotteryTicket(tickets,n);
    System.out.println(result);
    in.close();
}
}
static int winningLotteryTicket(String[] tickets,int n)
 // tickets = array of all strings

 {



Please don't put comments in between a function declaration and the function body (which starts with {).  

The standard way to write this in Java is 

// tickets = array of all strings
static int winningLotteryTicket(String[] tickets, int n) {


This is shorter and easier to read.  

Some people prefer to put the { on a separate line.  That's not standard in Java.  But if you do do that, please put the { on the line immediately after the function declaration.  Put comments either before the function declaration or inside the function body.  

In Java, unlike C, you do not need to pass the length of the array separately.  Instead of n, you can just use tickets.length.  So 

    for (int i = 0; i &lt; tickets.length; i++) {
        for (int j = i + 1; j &lt; tickets.length; j++) {


This also has the advantage of being clearer than n.  I can immediately see that it is the length of the array that is the limitation.  And this avoids bugs where n is passed as something other than the length of the array.  


     if(i!=j)



This line is unnecessary.  The way that the inner for loop is declared, j is always greater than i.  


         b=check.contains(r);



This is inefficient.  It does a linear time search for each digit.  We can do a single linear time search that covers all the digits.  Consider 

public static final int RADIX = 10;

static boolean hasAllDigits(String ticketPair) {
    boolean[] found = new boolean[RADIX];
    for (char digit : ticketPair.toCharArray()) {
        found[digit - '0'] = true;
    }

    for (boolean digitFound : found) {
        if (!digitFound) {
            return false;
        }
    }

    return true;
}


So the original method could be just 

static int winningLotteryTicket(String[] tickets) {
    int count = 0;

    // the i and j loop are to create all possible combinations
    for (int i = 0; i &lt; tickets.length; i++) {
        for (int j = i + 1; j &lt; tickets.length; j++) {
            if (hasAllDigits(tickets[i] + tickets[j])) {
                count++;
            }
        }
    }

    return count;
}


If this still isn't fast enough, consider storing the digits found in each string.  E.g. instead of "129300455" store "0123459".  Then you can just use merge logic to check if two strings contain all the digits.