Advertisement
Guest User

Untitled

a guest
Nov 25th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5.  
  6. public class WinningTicket_06 {
  7.  
  8.     private static final String REGEX = "(?=.{20}).*?(?=(?<ch>[@#$^]))(?<match>\\k<ch>{6,}).*(?<=.{10})\\k<match>.*";
  9.     private static final Pattern PATTERN = Pattern.compile(REGEX);
  10.     private static final Pattern SEPARATOR = Pattern.compile("\\s*,\\s*");
  11.  
  12.     public static void main(String[] args) {
  13.         Scanner scan = new Scanner(System.in);
  14.         String[] tickets = SEPARATOR.split(scan.nextLine().trim());
  15.  
  16.         for (String ticket : tickets) {
  17.             if (ticket.length() != 20) {
  18.                 System.out.println("invalid ticket");
  19.             } else {
  20.                 Matcher matcher = PATTERN.matcher(ticket);
  21.                 if (matcher.matches()) {
  22.                     String match = matcher.group("match");
  23.                     System.out.printf("ticket \"%s\" - %d%s%s%n",
  24.                             ticket, match.length(), match.charAt(0),
  25.                             (match.length() == 10) ? " Jackpot!" : "");
  26.                 } else {
  27.                     System.out.printf("ticket \"%s\" - no match%n", ticket);
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement