thieumao

Super Reduced String

Mar 28th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Solution {
  4.  
  5.     static boolean isContinue = true;
  6.  
  7.     static String reduced_string(String s){
  8.         int length = s.length();
  9.         if (length < 2) {
  10.             return s;
  11.         }
  12.         char des = s.charAt(0);
  13.         int count = 1;
  14.         String newString = "";
  15.         for (int i=1; i<length; i++){
  16.             if (des == s.charAt(i)){
  17.                 count++;
  18.             } else {
  19.                 if (count % 2 == 1) {
  20.                     newString = newString + des;
  21.                 }
  22.                 des = s.charAt(i);
  23.                 count = 1;
  24.             }
  25.         }
  26.         if (count % 2 == 1) {
  27.             newString = newString + des;
  28.         }
  29.         return newString;
  30.     }
  31.  
  32.     static boolean checkContinue(String s) {
  33.         int length = s.length();
  34.         if (length < 2) {
  35.             return false;
  36.         }
  37.         char des = s.charAt(0);
  38.         for (int i=1; i<length; i++){
  39.             if (des == s.charAt(i)){
  40.                 return true;
  41.             } else {
  42.                 des = s.charAt(i);
  43.             }
  44.         }
  45.         return false;
  46.     }
  47.  
  48.     static String super_reduced_string(String s){
  49.         String newString = s;
  50.         while (isContinue == true) {
  51.             newString = reduced_string(newString);
  52.             isContinue = checkContinue(newString);
  53.         }
  54.         if (newString.length() == 0) {
  55.             return "Empty String";
  56.         }
  57.         return newString;
  58.     }
  59.  
  60.     public static void main(String[] args) {
  61.         Scanner in = new Scanner(System.in);
  62.         String s = in.next();
  63.         String result = super_reduced_string(s);
  64.         System.out.println(result);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment