2

Will some one explain or make a program in c++ of this for me? Got assignment but don't know how to do it.

Question: You are given a set of strings which contain only as and bs, your program should be able to check whether each string has the same number of as and bs in it or not.

e.g. The program will respond true if it get {ab, aabb, aaabbbb, bbbaaa} and say false when it gets {aab, bbba, aaabbbb}

Solve it using stack

#include <iostream>
#include <string>
#include <stack>
#include <algorithm>

using namespace std;

int count1 = 0;
int count2 = 0;

bool isInLanguageL (string w);

int main()

{
    string input;

    cout << "Input any string; ";
    getline(cin,input);

    if (input.length() % 2 != 0)
        cout <<"Pattern entered does not match the language ";
    else
        isInLanguageL(input);

    return 0;
}

bool isInLanguageL (string w)
{
    stack<string> word1, word2;
    string a, b;

        for (unsigned i = 0; i < w.length()/2; i++)
    {
           a = w.at(i);
           word1.push(a);

    }

    reverse(w.begin(), w.end());

    for (unsigned i = 0; i < w.length()/2; i++)
    {
           b = w.at(i);
           word2.push(b);

    }


while(!word1.empty() && !word2.empty())
{


    word1.pop();
    count1 = count1++;
    word2.pop();
    count2 = count2++;

}

if(count1 == count2)
    return true;
else
    return false;

}
Mazhar Jr.
  • 33
  • 4
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/225500/discussion-on-question-by-mazhar-jr-check-if-every-string-in-a-set-contains-equ). – Samuel Liew Dec 04 '20 at 01:41

2 Answers2

2

This solution is using stack, please refer to the comments written in the code. If you have any doubt you can comment them.

Code:

#include <iostream>
#include <stack>
#include <string>
using namespace std;

void checkString(string s) {

    if (s.size() % 2 != 0) {
        cout << "Doesn't satisfy the conditon\n";
        return;
    }

    stack<char> st;
    int n = s.size();

    for (int i = 0; i < n; ++i) {

        /*
            case - 1 : If the stack is empty you can directly push the current character into the stack
            case - 2 : If there are elements present in the stack, then if the current character is equal to the top character on the stack then we can push the current character
                       beacuse we didn't find any new character to match them.
        */
        if (st.empty() || (st.top() == s[i])) {
            st.push(s[i]);
        }

        /*
            case-3 : If the stack is not emtpy and current character is different from the top character on the stack then we found a match like a-b (OR) b-a, so then we will
                     remove the top element from the stack and move to next character of the string
        */
        else if (st.top() != s[i]) {
            st.pop();
        }
    }

    /*
        case - 1 : After iterating through all the characters in the string, if we find the stack is emtpy then we can say all characters are not matched
        case - 2 : If stack is emtpy, then that means all the characters are matched.
    */
    (st.empty()) ? (cout << "Yes, satisfies the conditon\n") : (cout << "Doesn't satisfy the conditon\n");
}

int main() {

    string s = "";

    cin >> s;
    checkString(s);
    return 0;
}
Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16
1

Your solution has a number of mistakes that you should probably solve by using a debugger. Here's a reference.


This solution doesn't use a stack as you asked for, but you can write this function that uses algorithms to solve your problem:

namespace rs = std::ranges;

bool all_equal_as_and_bs(auto const & strings) 
{
  return rs::all_of(strings, [](auto const & string) 
         {
             return rs::count(string, 'a') == rs::count(string, 'b');
         });
}

And use it like this:

all_equal_as_and_bs(std::vector<std::string>{"ab", "aabb", "aaabbb", "bbbaaa"}); // true
all_equal_as_and_bs(std::vector<std::string>{"aab", "bba", "aaabbbb", "bbbaaa"}); // false
cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    It's a useful answer, have an upvote - I'll clean up my first comment. – Bathsheba Dec 03 '20 at 16:30
  • How about including the statement `using pointless = std::stack;` somewhere in the answer? (Intentionally non-instantiable but I believe the compiler must compile it - if anything that will wind up the professor!) – Bathsheba Dec 03 '20 at 16:33
  • 1
    @Bathsheba I thought of it, but I don't like being too cheeky in answers :) Your comment should suffice ;) Besides, while a stack is pointless here, I assume the OP is at that point in their class, and this question is just to give them practice with stacks. Also, I intentionally provided a C++20 solution, so the OP can't submit it directly. I'm unaware of any institution that actually has a testing framework that compiles C++20 yet. – cigien Dec 03 '20 at 16:35
  • Silly exercise though. If you want practise with stacks then build an expression parser. – Bathsheba Dec 03 '20 at 16:39
  • 1
    @Bathsheba Absolutely. But I do some teaching myself, and I'm confident this is just a misguided exercise to teach a concept. Happens a lot unfortunately. There are simpler, and useful cases for stacks than building an expression parser though :) – cigien Dec 03 '20 at 16:41
  • @Bathsheba Yes just studying in some bad university had to gone through these kind of stupid things – Mazhar Jr. Dec 03 '20 at 16:44
  • 1
    @cigien: My first use of a stack was building something to colour in an irregular shape with "islands" in it - in Z80 machine code. Happy days. Yup, simpler than an expression parser. – Bathsheba Dec 03 '20 at 16:47
  • 1
    @Bathsheba Oh my, Z80 machine code? You've been in the game too long ;) – cigien Dec 03 '20 at 16:50