HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Comparing a string using a stack

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
usingstringcomparingstack

Problem

Let L be the language defined as follows:

  • The words are made up of strings of a’s followed by b’s.



  • The number of a’s is always equal to the number of b’s.



  • Examples of words that belong to L are: ab, aabb, aaabbb etc...



One way to test if a word w belong to this language is to use a stack to check if the number of a’s balances the number of b’s.

This is what I though of doing:

  • Check if the length string is even



  • If it is send input to the function



  • Divide length by two and push the a's onto a stack



  • Reverse the string



  • Divide length by two and push the b's onto a stack



  • While the stack isn't empty pop each and store the count of each



  • Compare the count and if they are equal then return 0 or if not return 1



Please see below for the program I implemented:

#include 
#include 
#include 
#include 

using namespace std;

int count1 = 0;
int count2 = 0;

bool isInLanguageL (string w);

int main()

{
    string input;

    cout  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;

}


The issue I have with this is despite it working correctly I would appreciate and opinion on it as I feel they may have been another way to approach handling the strings although after racking my brains this was the best solution I could come up with.

What seems a bit silly to me is where I am comparing counts of the two stacks as it's pretty obvious based on the fact, that I'm not allowing odd numbers to pass into the function that the counts will always be equal, which would also be due to the division by 2 in each iteratio

Solution

At least to me, this seems like an excessively complex design.

I'd guess the intent is to create something on the order of a pushdown automata. In other words, the code processes a stream of incoming characters one at a time, with no global view of all the incoming data, and (almost) no memory other than the stack.

The obvious way to do this would be to read a character of input. If it's an 'a', push it onto the stack, and repeat. If it's a 'b', pop one 'a' from the stack, and repeat:

stack inputs;

char ch;

while ((ch = getinput()) == 'a')
    inputs.push(ch);

while (ch == 'b') {
    if (inputs.empty())
        error("Mismatch: more 'b's than 'a's");
    inputs.pop(ch);
    ch = getinput();
}

if (!inputs.empty())
    error("mismatch: more 'a's than 'b's");


Note that we're not really using any of the data we push onto the stack though--we're basically just using its depth (which can lead to an even simpler design, but on that doesn't appear to fit the specifications).

Code Snippets

stack<char> inputs;

char ch;

while ((ch = getinput()) == 'a')
    inputs.push(ch);

while (ch == 'b') {
    if (inputs.empty())
        error("Mismatch: more 'b's than 'a's");
    inputs.pop(ch);
    ch = getinput();
}

if (!inputs.empty())
    error("mismatch: more 'a's than 'b's");

Context

StackExchange Code Review Q#124106, answer score: 2

Revisions (0)

No revisions yet.