-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecode_string.cpp
More file actions
86 lines (68 loc) · 1.95 KB
/
decode_string.cpp
File metadata and controls
86 lines (68 loc) · 1.95 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// Created by Mayank Parasar on 2020-01-26.
//
/*
* Given a string with a certain rule: k[string] should be expanded to string k times.
* So for example, 3[abc] should be expanded to abcabcabc. Nested expansions can happen,
* so 2[a2[b]c] should be expanded to abbcabbc.
* */
#include <iostream>
#include <vector>
#include <string>
#include <ctype.h>
#include <stack>
using namespace std;
string decode_string(string str) {
int repeat = 1;
int indx = 0;
int first_brace=0;
int last_brace=0;
stack<char> s;
for(auto i : str) {
if((isdigit(i)) && (s.size() == 0)) {
repeat = isdigit(i) ? (i - '0') : repeat;
}
if(i == '[') {
if(s.size() == 0)
first_brace = indx;
s.push('[');
}
else if(i == ']') {
s.pop();
if(s.size() == 0)
last_brace = indx;
}
indx++;
}
// now you have index of first and last brace of the string.
// extract the string and send it to this function again....
int start = (first_brace+1);
int len = last_brace - first_brace - 1;
string str2;
if (len > 0) {
// get the left sub string and right substring if any,
string left_substring = str.substr(0, (first_brace-1));
string right_substring = str.substr(last_brace+1);
string string_2 = decode_string(str.substr(start, len));
// then do the concatination via for loop for 'repeat' times here
// reconstruct the string here..
// string string2;
for (int ii = 0; ii < repeat; ii++) {
str2 += string_2;
}
str2 = left_substring + str2 + right_substring;
return (str2);
}
else {
for (int ii = 0; ii < repeat; ii++) {
str2 += str;
}
return (str2);
}
}
int main() {
string str = "2[a2[bd]c]";
cout << decode_string(str);
// cout << str+2;
return 0;
}