Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar02 / task01.cpp
diff options
context:
space:
mode:
Commitf5524d158e7929ffa57c61599b074759e5d9d8c3 (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-10-12 15:46:47 +0300
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2022-10-12 15:46:47 +0300
Filesbd875cdd4db8561793fee65459c48a07977ecbc1 /Seminar02/task01.cpp
Data-structures-and-algorithms-f5524d158e7929ffa57c61599b074759e5d9d8c3.tar
Data-structures-and-algorithms-f5524d158e7929ffa57c61599b074759e5d9d8c3.tar.gz
Data-structures-and-algorithms-f5524d158e7929ffa57c61599b074759e5d9d8c3.zip
Parent51d955fbdada4400c8efd504d637b4ece6fcee22 (diff)
fix corner case
1 files changed, 21 insertions, 17 deletions
-rw-r--r--Seminar02/task01.cpp38
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<iostream>
std::string removeStars(const std::string& s) {
- std::stack<char> 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<char> 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() {