Remove Consecutive Duplicates
aabccbaa
abcba
public class Solution {
public static String removeConsecutiveDuplicates(String str) {
str=str+" ";
int l=str.length();
String s="";
for(int i=0;i<l-1;i++)
{
char ch1=str.charAt(i);
char ch2=str.charAt(i+1);
if(ch1!=ch2)
{
s=s+ch1;
}
}
return s;
}
}
Comments
Post a Comment