Advertisement
Guest User

Untitled

a guest
Jun 14th, 2021
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. package com.telerikacademy;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.  
  6.     private static int longestNonMatching(String input) {
  7.  
  8.         String[] inputArr = input.split("");
  9.  
  10.         int longest = 1;
  11.         int temp = 1;
  12.  
  13.         for (int i = 1; i < inputArr.length; i++) { // Iterate over all input chars
  14.             if (inputArr[i - 1].matches("[^a-zA-Z.0-9\\s]")) { // Check only those that don't match the regex
  15.                 if (inputArr[i].equals(inputArr[i - 1])) { // If tested char is same as the one before it - increase temp
  16.                     temp++;
  17.                 } else {
  18.                     longest = Math.max(longest, temp);
  19.                     temp = 1;
  20.                 }
  21.             }
  22.         }
  23.  
  24.         if (longest == 1) {
  25.             if (input.matches("[^a-zA-Z.0-9\\s]")) {
  26.                 return 1;
  27.             } else {
  28.                 return 0;
  29.             }
  30.         } else {
  31.             return longest;
  32.         }
  33.     }
  34.  
  35.     public static void main(String[] args) {
  36.         Scanner scanner = new Scanner(System.in);
  37.         System.out.print(longestNonMatching(scanner.nextLine()));
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement