Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string decodeString(string s) {
- int idx = 0;
- string ans = "";
- while (idx < (int) s.size()) {
- if (s[idx] >= 'a' && s[idx] <= 'z') {
- ans += s[idx++];
- } else {
- ans += decodeString(s, idx);
- }
- }
- return ans;
- }
- private:
- string decodeString(string &s, int &idx) {
- int k = 0;
- while (idx < (int) s.size() && s[idx] >= '0' && s[idx] <= '9') {
- k *= 10;
- k += s[idx++] - '0';
- }
- idx++;
- string inside = "";
- while (idx < (int) s.size() && s[idx] != ']') {
- if (s[idx] >= 'a' && s[idx] <= 'z') {
- inside += s[idx++];
- } else {
- inside += decodeString(s, idx);
- }
- }
- string ans = "";
- while (k--) {
- ans += inside;
- }
- idx++;
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement