Remove Consecutive Duplicates

 

For a given string(str), remove all the consecutive duplicate characters.

Sample Input 1:
aabccbaa
Sample Output 1:
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

Popular posts from this blog

Minimum Length Word

Check Number Sequence