Removing Consecutive Characters in each Iteration shows Unexpected error

Home / Uncategorized / Removing Consecutive Characters in each Iteration shows Unexpected error

Question:
How to remove Consecutive Characters at each Iteration.. Below is the screenshot that explains the question with more details

MySolution

1)Initially I checked whether there are any Consecutive characters. If yes,Then,remove all the consecutive characters and when there are no consecutive characters add the remaining characters to another String.

2)If no Consecutive Characters just simply increment it.
public static void print(){
String s1="aabcccdee"; I have taken a sample test case
String s2="";
for(int i=0;i Answer:
Try something like this:public static void print() {
String s1 = "abcccbd"; // I have taken a sample test case
String s2 = "";
while (!s1.equals(s2)) {
s2 = s1;
s1 = s1.replaceAll("(.)\1+", "");
}
System.out.println(s1);
}
Read more

Leave a Reply

Your email address will not be published. Required fields are marked *