From f5524d158e7929ffa57c61599b074759e5d9d8c3 Mon Sep 17 00:00:00 2001 From: Stoycho Kyosev Date: Wed, 12 Oct 2022 15:46:47 +0300 Subject: fix corner case --- Seminar02/task01.cpp | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) (limited to 'Seminar02/task01.cpp') diff --git a/Seminar02/task01.cpp b/Seminar02/task01.cpp index 3c98376..dfa97d7 100644 --- a/Seminar02/task01.cpp +++ b/Seminar02/task01.cpp @@ -3,24 +3,28 @@ #include std::string removeStars(const std::string& s) { - std::stack st; - - for(char c : s) { - if(c == '*') - st.pop(); - else - st.push(c); - } - - std::string res(st.size(), '\0'); - size_t idx = st.size() - 1; + std::stack st; - while(!st.empty()) { - res[idx--] = st.top(); - st.pop(); - } - - return res; + for (int i = 0; i < s.size(); i++) { + if (s[i] == '*') { + if (st.empty()) + throw "invalid expr"; + else + st.pop(); + } + else + st.push(s[i]); + } + + std::string res(st.size(), '\0'); + size_t idx = st.size() - 1; + + while (!st.empty()) { + res[idx--] = st.top(); + st.pop(); + } + + return res; } int main() { -- cgit v1.3.1-23-gd82d