Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class WinningTicket_06 {
- private static final String REGEX = "(?=.{20}).*?(?=(?<ch>[@#$^]))(?<match>\\k<ch>{6,}).*(?<=.{10})\\k<match>.*";
- private static final Pattern PATTERN = Pattern.compile(REGEX);
- private static final Pattern SEPARATOR = Pattern.compile("\\s*,\\s*");
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String[] tickets = SEPARATOR.split(scan.nextLine().trim());
- for (String ticket : tickets) {
- if (ticket.length() != 20) {
- System.out.println("invalid ticket");
- } else {
- Matcher matcher = PATTERN.matcher(ticket);
- if (matcher.matches()) {
- String match = matcher.group("match");
- System.out.printf("ticket \"%s\" - %d%s%s%n",
- ticket, match.length(), match.charAt(0),
- (match.length() == 10) ? " Jackpot!" : "");
- } else {
- System.out.printf("ticket \"%s\" - no match%n", ticket);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement