Write a pseudocode function that uses a stack to determine w
Write a pseudocode function that uses a stack to determine whether a string is in the language L, where
 L = {s : s contains equal numbers of A’s and B’s}.Using STL stack class, implement in C++ the pseudocode function and Write a main() program to test the function.
Solution
/*
Write a pseudocode function that uses a stack to determine whether a string is in the language L, where
 L = {s : s contains equal numbers of A’s and B’s}.Using STL stack class, implement in C++ the pseudocode function and Write a main() program to test the function.
 */
/*psudo code
function whether_string_is_in_L(str){
   
    initialize a stack
   iterate over all the char of the string
        if char is \'a\' or \'b\' {
            if the stack is empty and
                push \'a\' or \'b\'
            else
                last = peek top of stack
                if last == current char{
                    push current char in stack
                }else{
                    pop from the stack
                }
        }
      
       
   if at the end, the length of stack is 0 i.e stack is empty ,
        the string belongs to L
    otherwise not
 }
*/
 #include <iostream> // std::cout
 #include <stack> // std::stack
 #include <vector> // std::vector
 #include <deque> // std::deque
 using namespace std;
 #include <string>
int whether_string_is_in_L(string s){
    stack<char> mystack;
   for (int i = 0; i < s.length(); ++i)
    {
       if(s[i] == \'a\' || s[i] == \'b\'){
          if( mystack.empty() ) mystack.push(s[i]);
           else{
              if(s[i] == mystack.top()){
                   mystack.push(s[i]);
               }else{
                   mystack.pop();
               }
           }
       }
    }
   if(mystack.empty()){
       cout << \"string \" << s << \" belongs to family L\" << endl;
    }else{
       cout << \"string \" << s << \" does not belongs to family L\" << endl;
    }
return 1;
}
int main ()
 {
   
string s = \"aabssssaawss\";
whether_string_is_in_L(s);
s = \"a\";
whether_string_is_in_L(s);
 s = \"b\";
whether_string_is_in_L(s);
s = \"ab\";
whether_string_is_in_L(s);
s = \"ababab\";
whether_string_is_in_L(s);
s = \"ababaaaa\";
whether_string_is_in_L(s);
return 0;
 }


