Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/*Write either a C++ function int triplets(stack<string> words) or a Java method public static
 * int triplets(Stack<String> words) that take as the argument a stack of strings and count how
 * many elements are included in the stack exactly three times. You should use objects of type
 * set<string> in C++ or TreeSet<String> in Java. The set operations are given below (refer to
 * Problem 1 for the stack operations):
*/


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

string popped="";
int triplets(stack<string>words)
{
    set<string>set1{"wow", "AUA", "wow", "AUA", "Data  Structures", "AUA", "OOP", "wow"} ;
    for(const auto &count: set1)
    {
        words.push(count);

    }
    for(int i=0; i<words.size(); i++)
    {

        for(int current=i; current<words.size(); current++)
        {
            popped=words.top();

        }
    }

}


What I have tried:

I tried to solve it with queue comparing front to rear but it was more difficult/
Posted
Updated 23-Sep-17 10:23am
Comments
Richard MacCutchan 23-Sep-17 10:27am    
You are just copying your original stack to a new one but not doing anything useful with it. You need to create some new lists (stacks?) for each unique word. Once you have split them out you can then count them to see which list contains three entries. Also the set of words is the parameter sent in to your function so you do not need to create it, that should be done by the caller of the function.
Member 13277493 23-Sep-17 11:51am    
thank you very much for answer, but can you explain what do you mean by splitting them out? :)

1 solution

You could first insert all the strings of the stack into the set. Then make two nested loops: in the outer iterate over the set items, in the inner over the stack items, counting the matches.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900