This problem is like the inverse of #222, so it is important. It also helps practicing parsing.
Algorithm:
As long as we don't see any numbers, we add the current characters to our result, we don't need to check for brackets because they only come after numbers. When we see a digit, we try to get its full value, as it can consist of more than a single digit. After that we increment the index to skip the bracket, then recursively call the function to parse the inner of the bracket. The parsing loop then should stop when it encounters a closing bracket, or it reaches the end of the string.
Once we have read inner of some brackets, we increment the index to skip the closing brackets. We add it to the result as many times as we have previously read before the brackets, thus adding it as if it was multiplied.
Problem: https://leetcode.com/problems/decode-string/
This problem is like the inverse of #222, so it is important. It also helps practicing parsing.
Algorithm:
As long as we don't see any numbers, we add the current characters to our result, we don't need to check for brackets because they only come after numbers. When we see a digit, we try to get its full value, as it can consist of more than a single digit. After that we increment the index to skip the bracket, then recursively call the function to parse the inner of the bracket. The parsing loop then should stop when it encounters a closing bracket, or it reaches the end of the string.
Once we have read inner of some brackets, we increment the index to skip the closing brackets. We add it to the result as many times as we have previously read before the brackets, thus adding it as if it was multiplied.