0

The input string is 'aabbbcabcaa' and the output string should be 'cabc'.

I tried below code but it is not giving me required output:

public class removeConsecativeDuplicates {

    public static void main(String[] args) {
        String input = "aabbbcabcaa";
        System.out.println(removeConsecativeDuplicat(input));
    }

    public static String removeConsecativeDuplicat(String input) {
        StringBuilder result = new StringBuilder();
        for (int i = 2; i < input.length(); i++) {
            if (input.charAt(i) != input.charAt(i - 1) & input.charAt(i - 1) != input.charAt(i - 2)) {
                result.append(input.charAt(i));
            }
        }
        return result.toString();
    }
}
2
  • 1
    Try to use a separate local variable to store past characters. Commented Jan 19, 2024 at 15:18
  • 3
    In Java, the first character in a String is at position 0 rather than position 1. Commented Jan 19, 2024 at 15:29

4 Answers 4

1

In one line:

"aabbbcabcaa".replaceAll("(.)\\1+", "")

Inspired by the accepted answer to this question.

Also refer to this.

Sign up to request clarification or add additional context in comments.

Comments

0

Let's look what will happen for the input string "aab". the for loop will run once with i = 2 the condition with the correct values will be 'b' != 'a' & 'a' != 'a' which evaluates to false and so nothing will be appended to result. If you take the input string "abc" instead, the condition will be true but you skipped the first 2 characters so the output will only be "c".

Instead of comparing the characters with each other, your approach here should be to count how many consecutive characters there are and add them to the result string, if there is only one.

char lastchar = input.charAt(0); //check beforehand that the string isn't empty
int count = 1;
for (int i = 1; i <= input.length(); i++) {
   if (input.charAt(i) == lastchar) {
      count += 1;
   } else {
        if (count == 1) {
            result.append(lastchar);
        }
        count = 0;
        lastchar = input.charAt(i);
    }
}
if (count == 1) {
    result.append(lastchar);
}

Comments

0

This is a neat problem; most of the "remove duplicates" exercises intend that a single character from each set of duplicates is left behind, but in this case the problem is harder -- if the character is duplicated, completely remove it. You're on the right track but your logic isn't quite right. You want to take each character in turn, look backwards one and forwards one, if either are the same character then skip it. otherwise print:

public class removeConsecativeDuplicates {
    public static void main(String[] args) {
        String input = "aabbbcabcaa";
        System.out.println(removeConsecutives(input));
    }

    private static boolean isSame(String input,int idx1,int idx2) {
        if (idx1 < 0 || idx2 >= input.length()) return false; // catch first and last characters
        return input.charAt(idx1) == input.charAt(idx2);
    }
    
    public static String removeConsecutives(String input) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            if (isSame(input, i, i+1)) { continue; }
            if (isSame(input, i-1, i)) { continue; }
            result = result.append(input.charAt(i));
        }
        return result.toString();
    }
}

Comments

-1

Check again to your IDE and try this solution:

public class removeConsecativeDuplicates {

    public static void main(String[] args) {
        String input = "aabbbcabcaa";
        System.out.println(removeConsecativeDuplicates(input));
    }

    public static String removeConsecativeDuplicates(String input) {
        StringBuilder result = new StringBuilder();
        for (int i = 2; i < input.length(); i++) {
            if (input.charAt(i) != input.charAt(i - 1) & input.charAt(i - 1) != input.charAt(i - 2)) {
                result.append(input.charAt(i));
            }
        }
        return result.toString();
    }
}

1 Comment

What should the user check in their IDE? Is this a new solution or a restatement?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.