1047. Remove All Adjacent Duplicates In String

1047. Remove All Adjacent Duplicates In String

class Solution {
    public String removeDuplicates(String S) {
        if(S == null || S.isEmpty()) return "";
        if(S.length() ==1) return S;
        for (int i = 0; i < S.length() - 1; i++){
            if(S.charAt(i) == S.charAt(i+1))
                return removeDuplicates(S.substring(0,i)+S.substring(i+2));
        }
        return S;
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s