Easy
1 class SignatureDecorator {
2 public:
3 string applyDecoration(string name, vector <string> commands, vector <string> decorations) {
4 int n = commands.size();
5 for (int i = 0; i < n; i++) {
6 if (commands[i] == "prepend") {
7 name = decorations[i] + name;
8 } else if (commands[i] == "append") {
9 name = name + decorations[i];
10 } else { // commands[i] == "surround"
11 string t = decorations[i];
12 reverse(t.begin(), t.end());
13 name = decorations[i] + name + t;
14 }
15 }
16 return name;
17 }
18 }
Medium
1 class ParameterSubstitution {
2 public:
3 string processParams(string code, vector <string> params) {
4 string ret;
5 for (int i = 0; i < code.size(); i++) {
6 if (code[i] == '$') {
7 i++;
8 if (!isdigit(code[i])) {
9 ret.push_back('$');
10 i--;
11 continue;
12 }
13 int n = code[i] - '0';
14 if (n == 0 || n > params.size()) {
15 ret.push_back('$');
16 ret.push_back(code[i]);
17 continue;
18 }
19 int _n = 0;
20 if (isdigit(code[i+1])) {
21 while (i < code.size() && isdigit(code[i++])) {
22 _n = n;
23 n = n * 10 + code[i] - '0';
24 if (n > params.size()) {
25 ret.append(params[_n-1]);
26 i--;
27 break;
28 }
29 }
30 } else {
31 ret.append(params[n-1]);
32 }
33 } else {
34 ret.push_back(code[i]);
35 }
36 }
37 return ret;
38 }
39 }