Advertisement
DNNdrago

3. Largest Sequence of Equal Strings

May 23rd, 2014
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class _03_LargestSequenceOfEqualStrings {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner (System.in);
  8.         String[] words = sc.nextLine().split(" ");
  9.        
  10.         int countOfEqualStrings = 1;
  11.         int maxcountOfEqualStrings = 1;
  12.         String word = words[0] + "";
  13.         for (int i = 0; i < words.length - 1; i++) {
  14.             //Next word is equal
  15.             if (words[i].equals(words[i+1])) {
  16.                 countOfEqualStrings++;
  17.                 if (i == words.length - 2) {
  18.                     if (maxcountOfEqualStrings < countOfEqualStrings) {
  19.                         word = words[i] + "";
  20.                         maxcountOfEqualStrings = countOfEqualStrings;
  21.                     }
  22.                 }
  23.             }
  24.             //Next word is not equal
  25.             else {
  26.                 if (maxcountOfEqualStrings < countOfEqualStrings) {
  27.                     word = words[i] + "";
  28.                     maxcountOfEqualStrings = countOfEqualStrings;
  29.                 }
  30.                 countOfEqualStrings = 1;
  31.             }
  32.         }
  33.        
  34.         for (int i = 0; i < maxcountOfEqualStrings; i++) {
  35.             System.out.print(word + " ");
  36.         }
  37.  
  38.     }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement