rudi2004

06. Winning Ticket java

Dec 5th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class WinningTicket {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.  
  8.         String input = scanner.nextLine();
  9.         input = input.replace(" ", "");
  10.         String[] ticket = input.split(",");
  11.  
  12.         for (int i = 0; i < ticket.length; i++) {
  13.             if (ticket[i].length() != 20) {
  14.                 System.out.println("invalid ticket");
  15.             } else {
  16.                 String left = ticket[i].substring(0, 10);
  17.                 String right = ticket[i].substring(10);
  18.                 String winingSymbol = findSymbol(left);
  19.                 if (winingSymbol == null) {
  20.                     System.out.printf("ticket \"%s\" - no match%n", ticket[i]);
  21.                     continue;
  22.                 }
  23.                 int bestSequence = findMaxSequence(left, winingSymbol.charAt(0));
  24.                 StringBuilder bestCombination = new StringBuilder();
  25.                 for (int j = 0; j < bestSequence; j++) {
  26.                     bestCombination.append(winingSymbol);
  27.                 }
  28.  
  29.                 if (left.contains(bestCombination) && right.contains(bestCombination) && bestCombination.length() != 10) {
  30.                     System.out.printf("ticket \"%s\" - %d%s%n", ticket[i], bestSequence, winingSymbol);
  31.                 } else if (left.contains(bestCombination) && right.contains(bestCombination)) {
  32.                     System.out.printf("ticket \"%s\" - %d%s Jackpot!%n", ticket[i], bestSequence, winingSymbol);
  33.  
  34.                 }else {
  35.                     System.out.printf("ticket \"%s\" - no match%n", ticket[i]);
  36.                 }
  37.             }
  38.  
  39.         }
  40.     }
  41.  
  42.     static String findSymbol(String input) {
  43.         int countAT = 0;
  44.         int countNumberSign = 0;
  45.         int countDolar = 0;
  46.         int countCaret = 0;
  47.  
  48.         for (int i = 0; i < input.length(); i++) {
  49.             if (input.charAt(i) == '@') {
  50.                 countAT++;
  51.             } else if (input.charAt(i) == '#') {
  52.                 countNumberSign++;
  53.             } else if (input.charAt(i) == '$') {
  54.                 countDolar++;
  55.             } else if (input.charAt(i) == '^') {
  56.                 countCaret++;
  57.             }
  58.         }
  59.  
  60.         if (countAT >= 6) {
  61.             return "@";
  62.         } else if (countNumberSign >= 6) {
  63.             return "#";
  64.         } else if (countDolar >= 6) {
  65.             return "$";
  66.         } else if (countCaret >= 6) {
  67.             return "^";
  68.         }
  69.         return null;
  70.     }
  71.  
  72.     static int findMaxSequence(String ticket, char symbol) {
  73.         int maxSequence = 1;
  74.         int count = 1;
  75.         for (int i = 1; i < ticket.length(); i++) {
  76.             char currentChar = ticket.charAt(i);
  77.             char previousChar = ticket.charAt(i - 1);
  78.             if (currentChar == symbol && currentChar == previousChar) {
  79.                 count++;
  80.                 if (count > maxSequence) {
  81.                     maxSequence = count;
  82.                 }
  83.             } else {
  84.                 count = 1;
  85.             }
  86.         }
  87.         return maxSequence;
  88.     }
  89. }
  90.  
Add Comment
Please, Sign In to add comment