I apologise if the title is not very descriptive. If someone can recommend a clearer one I'll edit it. I have an exercise question as:
The language L={anbn} where n ≥ 1, is a language of all words with the following properties:
- The words consist of strings of a’s followed by b’s.
- The number of b’s is equal the number of a’s.
- Examples of words that belong to L are:
ab, where n=1;
aabb, where n=2;
aaabbb, where n=3;
aaaabbbb, where n=4.
One way to test if a word w belong to this language L is to use a stack to check if the number of a’s balances the number of b’s. Use the following header and write a function isInLanguageL2 that uses a stack to test if any word belongs to L. If w belongs to L then the isInLanguageL2 should return true otherwise isInLanguageL2 should return false.
bool isInLanguageL(string w);
Note the following:
- Only words belonging to L should be accepted.
- The word bbaa does not belong to L.
- Do not count the number of a’s or b’s.
I've written the following code which solves the problem.
bool isInLanguageL2(string w){
linkedStackType<string> wordStack;
int size = w.length();
int a = 0;
int b = 0;
if(size % 2 != 0) //word length must be an equal number
return false;
else{
//read first half of word
for(int i = 0; i < size/2; i++){ //check if the first half consists of A's
if(w[i] != 'a' && w[i] != 'A')
return false;
else
wordStack.push(string(1, w[i])); //convert char to string and push to stack if the letter is valid
}
//read second half of word
for(int i = size/2; i < size; i++){ //check if the second half consists of B's
if(w[i] != 'b' && w[i] != 'B')
return false;
else
wordStack.push(string(1, w[i])); //convert char to string and push to stack if the letter is valid
}
}
//check number of A's and B's in the stack
while(!wordStack.isEmptyStack()){
if(wordStack.top() == "b" || wordStack.top() == "B"){
b++;
wordStack.pop();
}
else{
a++;
wordStack.pop();
}
}
//check if number of B's is equal to number of A's
if(b==a)
return true;
}
However, since the question says "Do not count the number of a’s or b’s.", I've instead created two stacks and just compared their lengths. Is there a way to do this using a single stack since the question mentions using a stack without explicitly counting the A's and B's to test the word.
bool isInLanguageL2(string w){
linkedStackType<string> wordStackA, wordStackB;
int size = w.length();
int a = 0;
int b = 0;
if(size % 2 != 0) //word length must be an equal number
return false;
else{
//read first half of word
for(int i = 0; i < size/2; i++){ //check if the first half consists of A's
if(w[i] != 'a' && w[i] != 'A')
return false;
else
wordStackA.push(string(1, w[i])); //convert char to string and push to stack if the letter is valid
}
//read second half of word
for(int i = size/2; i < size; i++){ //check if the second half consists of B's
if(w[i] != 'b' && w[i] != 'B')
return false;
else
wordStackB.push(string(1, w[i])); //convert char to string and push to stack if the letter is valid
}
}
//check if number of B's is equal to number of A's
if(wordStackA.length() == wordStackB.length());
return true;
}
Also, I have a follow up to this question. A sort of extension. Should I add it here or create a new question for it as it might make this question rather long.
a
, and pop when you see ab
. It's an error if the stack underflows or is not empty at the end. But that would allowabab
, for instance, which seems to be prohibited. \$\endgroup\$a
's on the stack until a non-a
character is found. Then, iterate through the remaining characters. For every remaining character, if it is not ab
or the stack is empty, return false. Otherwise, pop the stack. When there are no more characters, if the stack is not empty, return false. Otherwise, return true. \$\endgroup\$