Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class DuplicatedLetters {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String str = scan.nextLine();
- int operations = 0;
- ArrayList<Character> chars = new ArrayList<>();
- int length = str.length();
- for (int i = 0; i < length; i++) {
- chars.add(str.charAt(i));
- }
- for (int i = 0; i < chars.size() - 1; i++) {
- if (chars.get(i) == chars.get(i + 1)){
- operations++;
- chars.remove(i + 1);
- chars.remove(i);
- i = - 1;
- }
- }
- StringBuilder sb = new StringBuilder();
- for (char ch :
- chars) {
- sb.append(ch);
- }
- if (sb.length() > 0){
- System.out.println(sb);
- }
- else{
- System.out.println("Empty String");
- }
- System.out.printf("%d operations\n", operations);
- }
- }
Add Comment
Please, Sign In to add comment