0

Expected output is "ca" but I'm getting "aca". I have dry ran it, but do not understood why it is doing so. Please, can anyone help me in solving this?

#include<bits/stdc++.h>
using namespace std;
    
int main()
{
    string a = "abbaca";
    int i = 0;
    while(i < a.size()){
        if(a[i] == a[i+1]){
            a.erase(i, i+1);
            i = 0;
        }
        else{
            i++;
        }
    }
    cout << a;
    
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    Your code shows all the signs of being code for competitive coding site. While these sites may teach you problem solving skills they're not so good for learning how to write good C++. Don't use "using namespace", do not include . Anyway read documentation on methods you use e.g. : https://en.cppreference.com/w/cpp/string/basic_string/erase and check the meaning of the second parameter of erase (it is length not position). – Pepijn Kramer Jan 10 '22 at 04:06
  • 1
    It would be much more straightforward to copy the desired characters into a new string, than to try to modify the input string while iterating over it. – Chris Jan 10 '22 at 04:12
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/), [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/) – Remy Lebeau Jan 10 '22 at 04:17
  • *Expected output is "ca"* what? why? – n. m. could be an AI Jan 10 '22 at 04:24
  • @n.1.8e9-where's-my-sharem. Because it is expected to first remove `"bb"` leaving behind `"aaca"`, then remove `"aa"` leaving behind `"ca"` – Remy Lebeau Jan 10 '22 at 04:32
  • 1
    Note https://en.cppreference.com/w/cpp/algorithm/adjacent_find – Passerby Jan 10 '22 at 04:33
  • How is this a well-defined operation? The result depends on the order of removal. – n. m. could be an AI Jan 10 '22 at 05:30
  • Your algo is `O(n²)` (because of the `erase`) whereas it might be `O(n)`. – Jarod42 Jan 10 '22 at 09:46

2 Answers2

2

a.erase(i, i+1) is wrong.

The string::erase() method takes a starting index and a count, not a pair of indexes denoting a range, as you are thinking.

When removing the duplicate bs, i is 1, and you erase i+1=2 chars, thus "abbaca" becomes "aaca", which is correct. But then, the loop starts over, and when removing the duplicate as, i is 0, so you erase i+1=1 char, thus "aaca" becomes "aca", which is wrong.

You want to remove exact 2 chars each time, so use a.erase(i, 2) instead.

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

The function 'erase()' erases a part of the string content, shortening the length of the string.The second parameter in the erase function is the count, it means how many characters you want it to remove. If you want 'ca' as expected output you should mention 2 as count of characters to be removed. In the first case it becomes 2 so bb is removed but for 'aa' the count becomes as 1 so 'aca' is the output.

Below is the code for output 'ca', change this erase statement as shown:

    if(a[i]==a[i+1]){
        a.erase(i,2);
        i=0;
    }

keep the rest as same

Fazal Syed
  • 11
  • 2