fbinnzhivko

Untitled

Jan 9th, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.05 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class DuplicatedLetters {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.  
  8.         String str = scan.nextLine();
  9.  
  10.         int operations = 0;
  11.  
  12.         ArrayList<Character> chars = new ArrayList<>();
  13.  
  14.         int length = str.length();
  15.         for (int i = 0; i < length; i++) {
  16.             chars.add(str.charAt(i));
  17.         }
  18.  
  19.         for (int i = 0; i < chars.size() - 1; i++) {
  20.             if (chars.get(i) == chars.get(i + 1)){
  21.                 operations++;
  22.                 chars.remove(i + 1);
  23.                 chars.remove(i);
  24.                 i = - 1;
  25.             }
  26.         }
  27.  
  28.         StringBuilder sb = new StringBuilder();
  29.         for (char ch :
  30.                 chars) {
  31.             sb.append(ch);
  32.         }
  33.         if (sb.length() > 0){
  34.             System.out.println(sb);
  35.         }
  36.         else{
  37.             System.out.println("Empty String");
  38.         }
  39.  
  40.         System.out.printf("%d operations\n", operations);
  41.     }
  42. }
Add Comment
Please, Sign In to add comment