Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class WinningTicket {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- input = input.replace(" ", "");
- String[] ticket = input.split(",");
- for (int i = 0; i < ticket.length; i++) {
- if (ticket[i].length() != 20) {
- System.out.println("invalid ticket");
- } else {
- String left = ticket[i].substring(0, 10);
- String right = ticket[i].substring(10);
- String winingSymbol = findSymbol(left);
- if (winingSymbol == null) {
- System.out.printf("ticket \"%s\" - no match%n", ticket[i]);
- continue;
- }
- int bestSequence = findMaxSequence(left, winingSymbol.charAt(0));
- StringBuilder bestCombination = new StringBuilder();
- for (int j = 0; j < bestSequence; j++) {
- bestCombination.append(winingSymbol);
- }
- if (left.contains(bestCombination) && right.contains(bestCombination) && bestCombination.length() != 10) {
- System.out.printf("ticket \"%s\" - %d%s%n", ticket[i], bestSequence, winingSymbol);
- } else if (left.contains(bestCombination) && right.contains(bestCombination)) {
- System.out.printf("ticket \"%s\" - %d%s Jackpot!%n", ticket[i], bestSequence, winingSymbol);
- }else {
- System.out.printf("ticket \"%s\" - no match%n", ticket[i]);
- }
- }
- }
- }
- static String findSymbol(String input) {
- int countAT = 0;
- int countNumberSign = 0;
- int countDolar = 0;
- int countCaret = 0;
- for (int i = 0; i < input.length(); i++) {
- if (input.charAt(i) == '@') {
- countAT++;
- } else if (input.charAt(i) == '#') {
- countNumberSign++;
- } else if (input.charAt(i) == '$') {
- countDolar++;
- } else if (input.charAt(i) == '^') {
- countCaret++;
- }
- }
- if (countAT >= 6) {
- return "@";
- } else if (countNumberSign >= 6) {
- return "#";
- } else if (countDolar >= 6) {
- return "$";
- } else if (countCaret >= 6) {
- return "^";
- }
- return null;
- }
- static int findMaxSequence(String ticket, char symbol) {
- int maxSequence = 1;
- int count = 1;
- for (int i = 1; i < ticket.length(); i++) {
- char currentChar = ticket.charAt(i);
- char previousChar = ticket.charAt(i - 1);
- if (currentChar == symbol && currentChar == previousChar) {
- count++;
- if (count > maxSequence) {
- maxSequence = count;
- }
- } else {
- count = 1;
- }
- }
- return maxSequence;
- }
- }
Add Comment
Please, Sign In to add comment