Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .

0 like 0 dislike
1,982 views
in Online Assessments by Expert (144,420 points)

2 Answers

0 like 0 dislike
 
Best answer

image

Cisco Coding Question

by Expert (144,420 points)
0 like 0 dislike

c++ solution, modified from the solution https://leetcode.com/problems/decode-string/discuss/472087/0ms-C%2B%2B-solution-using-one-stack
string expandedString (string inputStr)
{
stack st;
for (int i = 0; i < inputStr.size(); i++) {
if (inputStr[i] != '}') {
st.push(inputStr[i]);
}
else {
// for calculating num
string number = "";
while (!st.empty() && isdigit(st.top())) {
number = st.top() + number;
st.pop();
}
st.pop(); // pop '{'
st.pop();// pop')'
int num = stoi(number);

 

        // get string to times
        string curr_str = "";
        while (st.top() != '('){
            curr_str = st.top() + curr_str;
            st.pop();
        }
        st.pop(); // pop '('
        
        while (num--) {
            for (int p = 0; p < curr_str.size(); p++) {
                st.push(curr_str[p]);
            }
        }
    }
}
string s = "";
while (!st.empty()) {
    s = st.top() + s;
    st.pop();
}
return s;

 

}

by Expert (144,420 points)
...