0

I have written a code to recursively remove the adjacent duplicates from a string

class Solution{
String rremove(String s) {
    // code here
    
    StringBuilder sb = new StringBuilder();
    int i = 0;
    
    while (i < s.length()) {
        char currentChar = s.charAt(i);
        int j = i + 1;
        // Find the index of the next different character
        while (j < s.length() && s.charAt(j) == currentChar) {
            j++;
        }
        // If adjacent duplicates found, skip them
        if (j - i > 1) {
            i = j;
        } else {
            sb.append(currentChar);
            i++;
        }
    }
    
    String result = sb.toString();
    // If result string is different from original, recursively call the function
    if (!result.equals(s)) {
        return rremove(result);
    }
    return result;
    
}

Now, I want to understand the time complexity of this code as the number of recursive calls is not fixed here, hence got quite confused looking at the code. Although, each method call is O(n), but with the depth of recursion, it gets uncertain. Hence, please help me understand the best case and worst case scenarion for this code.

The code is from GFG with this title "Recursively remove all adjacent duplicates "

5
  • 1
    When I try with "aaaabbbbbcccc" I get an empty string back. Commented May 7, 2024 at 19:35
  • 2
    @trincot I believe that is expected, as posted: "Recursively remove all adjacent duplicates" (again assuming that duplicates also include triplicates, ..) Commented May 7, 2024 at 19:35
  • OK, seems ambiguous. If I remove duplicates, then I don't regard the first occurrence as being the duplicate. Apparently, the interpretation is different from what I understood. But where is the asker? Commented May 7, 2024 at 19:36
  • @trincot agreed, extremely ambiguous regarding if the first occurrence should stay or not (I am just assuming the code is correct, but, as you already wrote, a recursive call would be useless if the first occurrence is not removed... unless it is meant to remove one single duplicate per call, leaving the third or further repetition to be remove in the next call{?}) Commented May 7, 2024 at 19:41
  • @trincot Yes, its expected to get empty string at the end, as we are recursively removing the duplicate. Commented May 8, 2024 at 4:41

1 Answer 1

1

If we exclude the recursive call from the analysis, the complexity is indeed O(𝑛). This is also the best case time complexity, i.e. when no recursive call is made because there are no adjacent duplicates in the input.

When we incorporate the recursive call, then the worst case occurs when you have a palindrome where the only duplicate character occurs at the center. For instance abababababbababababa. Now we have calls of rremove that get strings of length 𝑛, then 𝑛−2, 𝑛−4, ... 2, 0.

And so the worst-case time complexity is O(𝑛²).

Improving...

The worst-case time complexity can be improved by popping characters from the end of the StringBuilder instance when they are found equal to the current character.

Here is an implementation:

    static String rremove(String s) {
        StringBuilder sb = new StringBuilder();
        boolean skip = false;
        char currentChar = ' ';
        for (int i = 0; i < s.length(); i++) {
            if (!skip || s.charAt(i) != currentChar) {
                currentChar = s.charAt(i);
                skip = sb.length() > 0 && currentChar == sb.charAt(sb.length() - 1);
                if (skip) {
                    sb.setLength(sb.length() - 1);
                } else {
                    sb.append(currentChar);
                }
            }
        }
        return sb.toString();        
    }

This has linear time complexity.

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

2 Comments

Yes, with the palindromic string, the number of recursive calls will increase, and as you mentioned, it will loop like n, n-2, n-4. In that case, it should be O(n^2). Thanks for the explanation.. .guess i was missing such an example !!!
Just to complete the analysis I added a version that improves the worst-case time complexity.

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.