Skip to main content
added 76 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

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 atleast once etc

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<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();
}
}

}

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 atleast 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();
}

}

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<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();
}
}
Source Link

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 atleast 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();
}

}