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 123456879Output
5Explanation
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<n;i++)
{
for(int j=i+1;j<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<=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 < n; tickets_i++){
tickets[tickets_i] = in.next();
}
int result = winningLotteryTicket(tickets,n);
System.out.println(result);
in.close();
}
}