Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.telerikacademy;
- import java.util.Scanner;
- public class Main {
- private static int longestNonMatching(String input) {
- String[] inputArr = input.split("");
- int longest = 1;
- int temp = 1;
- for (int i = 1; i < inputArr.length; i++) { // Iterate over all input chars
- if (inputArr[i - 1].matches("[^a-zA-Z.0-9\\s]")) { // Check only those that don't match the regex
- if (inputArr[i].equals(inputArr[i - 1])) { // If tested char is same as the one before it - increase temp
- temp++;
- } else {
- longest = Math.max(longest, temp);
- temp = 1;
- }
- }
- }
- if (longest == 1) {
- if (input.matches("[^a-zA-Z.0-9\\s]")) {
- return 1;
- } else {
- return 0;
- }
- } else {
- return longest;
- }
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print(longestNonMatching(scanner.nextLine()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement