-2

I am solving a question which states: to change every '?' with 'a' in a string if doesn't contain if won't form consecutive 'a' else substitute with 'b', eg. a?b will be abb and not aab because here 2 a's are consecutive.

My problem is for i = 3 my string should be over- written with 'b ' according to my code it is entering into the desired block but the string does n't gets written with b, but in all the other case where it should be witten with 'a' it get's written .Help me out with these.

You can refer the problem statement from here to for better understanding my problem :https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/practice-problems/algorithm/exploring-ruins/

#include <iostream>
using namespace std;

int main() {
    string str;
    cin >> str;
    int n = str.size();

    for(int i = 0; i < str.size(); i++) {
        if(str[i] == '?') {
            if(i == 0) {
                if(str[i + 1] == 'a')
                    str[i] = 'b';
                else
                    str[i] = 'a';
                cout << "I am in if" << endl;
            } else if(i == n - 1) {
                if(str[i - 1] == 'a')
                    str[i] == 'b';
                else
                    str[i] == 'a';
                cout << "I am in if of  else if " << endl;
            } else {
                if(str[i + 1] == 'a' || str[i - 1] == 'a') {
                    str[i] == 'b';
                    cout << "I am in if  of  else " << endl;
                } else {
                    str[i] = 'a';
                    cout << "I am in else of else " << endl;
                }
            }
            cout << str[i] << endl;
        } else
            continue;
    }
    cout << str << endl;

    return 0;
}

Given string : ?ba??b desired output : ababab my output : aba?ab

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • There are multiple places which look as you intended to assign `str[i]`. However, instead you did compare (which is syntactically correct but without side-effect and hence useless). `==` ... is equal, `=` ... assign -> you know? – Scheff's Cat May 17 '19 at 16:14
  • Which specific branch do you think you're in when you should be assigning `b`? Can you step through it in a debugger? Did you get any of the debug output you added? What did it say? – Useless May 17 '19 at 16:15
  • This is trivial if you used STL algorithms to solve the problem. Functions such as `std::adjacent_find`, for instance. – PaulMcKenzie May 17 '19 at 16:15
  • With ful warnings enabled, your compiler would tell you this: [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/c51c8cfb15c101d3) e.g. line 25. – Scheff's Cat May 17 '19 at 16:18
  • May I suggest a different solution. Go through the string twice. First pass, replace all `?` with `a`. Second pass, replace all adjacent `aa` with `ab`. This will guarantee that you will not end up with a `?` character in your string. – PaulMcKenzie May 17 '19 at 16:23
  • 2
    And [here you go](https://coliru.stacked-crooked.com/a/92703fcc44e2f560). Isn't STL great? – PaulMcKenzie May 17 '19 at 16:25
  • @PaulMcKenzie in your solution, you will change the following input: "aaa?" to: "abab" instead of replace only the '?' char and return: "aaab". And your algorithm complexity is `O(mn)` where `m` is '?' count, and `n` is the string length. – Coral Kashri May 17 '19 at 16:44
  • @KorelK You can try it via the link. And yes, it becomes "abab". – Ted Lyngmo May 17 '19 at 16:52
  • @TedLyngmo I know, but it shouldn't do it :) – Coral Kashri May 17 '19 at 17:00
  • @KorelK :-D that's the spirit! – Ted Lyngmo May 17 '19 at 17:01
  • @KorelK ok, but all that really needs to be done is replace the character with 'a', and check the previous and next character (if it exists) and adjust if necessary. [Uses iterators](https://coliru.stacked-crooked.com/a/e37f52711f4331f7) – PaulMcKenzie May 17 '19 at 17:23
  • @PaulMcKenzie this is a nice solution, but there is one case where it still won't work: "?a" :) – Coral Kashri May 17 '19 at 17:47
  • ok, [this solution](https://coliru.stacked-crooked.com/a/8cd78e3a99fbfbdc) should work correctly. In any event, the high-level idea still holds -- change the letter to 'a', regardless of what the neighbors are, and then check the neighbors to see if the letter should be changed to 'b'. The OP's attempted solution is much more complicated than it should be. – PaulMcKenzie May 18 '19 at 03:20
  • I didn't use debugger because my debugger doesn't work ,so I code on online IDE jdoodle @ Scheff I didn't get you , @ Useless I should be in branch if(str[i + 1] == 'a' || str[i - 1] == 'a') { str[i] == 'b'; – Vardha Rathore May 18 '19 at 17:30
  • I didn't use debugger because my debugger doesn't work ,so I code on online IDE jdoodle @ Scheff I didn't get you , @ Useless I should be in branch if(str[i + 1] == 'a' || str[i - 1] == 'a') { str[i] == 'b';} of else brach ,@paulMckenzie I can't iterate loop twice it will increase my time complexity. I think my code and Idea is correct so why is it not working – Vardha Rathore May 18 '19 at 17:33
  • can any one tell me how to fix nx problem of debugger in code blocks here's my querry link :https://stackoverflow.com/questions/55473658/how-to-fix-codeblocks-debuggers-unknown-option-nx – Vardha Rathore May 18 '19 at 17:35

1 Answers1

0

It will be a lot easier for you if you would use functions to solve this problem.

bool check_neighbors_for_a(const string &str, size_t place) {
    bool result = false;
    if (place > 0) { // If there is a char before the current char
        result = str[place - 1] == 'a'; // If the previous char is 'a' result become true
    }
    if (place < str.size() - 1) { // If there is a char after the current char
        result = result || str[place + 1] == 'a'; // If the result has become true before this line, result will stay true. Else, result will be true if the next char is equal to 'a'.
        // For example: b?a => result = (false || 'a' == 'a')
        // For example: a?b => result = (true  || 'b' == 'a')
        // For example: a?a => result = (true  || 'a' == 'a')
    }
    return result;
}

void replace_questions_by_a(string &str) {
    for (size_t i = 0; i < str.size(); i++) {
        if (str[i] == '?') {
            if (check_neighbors_for_a(str, i)) { // If one of the neighbors is equal to 'a'
                str[i] = 'b'; // Place 'b' instead of '?'
            } else {
                str[i] = 'a'; // Place 'a' instead of '?'
            }
        }
    }
}
Coral Kashri
  • 3,436
  • 2
  • 10
  • 22