forked from gdscwce/hacktoberfest-2k25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecode_String.cpp
More file actions
42 lines (36 loc) · 960 Bytes
/
Copy pathDecode_String.cpp
File metadata and controls
42 lines (36 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
string decodeString(string s) {
stack<int> countStack;
stack<string> stringStack;
string current = "";
int k = 0;
for (char c : s) {
if (isdigit(c)) {
k = k * 10 + (c - '0'); // build the multiplier
} else if (c == '[') {
countStack.push(k);
stringStack.push(current);
current = "";
k = 0;
} else if (c == ']') {
string temp = current;
current = stringStack.top();
stringStack.pop();
int repeat = countStack.top();
countStack.pop();
while (repeat--) current += temp;
} else {
current += c;
}
}
return current;
}
int main() {
string s;
cout << "Enter encoded string: ";
cin >> s;
string decoded = decodeString(s);
cout << "Decoded string: " << decoded << endl;
return 0;
}