Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Solution {
- static boolean isContinue = true;
- static String reduced_string(String s){
- int length = s.length();
- if (length < 2) {
- return s;
- }
- char des = s.charAt(0);
- int count = 1;
- String newString = "";
- for (int i=1; i<length; i++){
- if (des == s.charAt(i)){
- count++;
- } else {
- if (count % 2 == 1) {
- newString = newString + des;
- }
- des = s.charAt(i);
- count = 1;
- }
- }
- if (count % 2 == 1) {
- newString = newString + des;
- }
- return newString;
- }
- static boolean checkContinue(String s) {
- int length = s.length();
- if (length < 2) {
- return false;
- }
- char des = s.charAt(0);
- for (int i=1; i<length; i++){
- if (des == s.charAt(i)){
- return true;
- } else {
- des = s.charAt(i);
- }
- }
- return false;
- }
- static String super_reduced_string(String s){
- String newString = s;
- while (isContinue == true) {
- newString = reduced_string(newString);
- isContinue = checkContinue(newString);
- }
- if (newString.length() == 0) {
- return "Empty String";
- }
- return newString;
- }
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String s = in.next();
- String result = super_reduced_string(s);
- System.out.println(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment