Decode a string encoded in the format "k[encoded_string]" where the encoded_string inside the brackets is repeated k times. The encoding can be nested. Return null for null input and empty string for empty input.
Rules:
- k is a positive integer
- encoded_string contains only letters and may include other encoded patterns
- Nesting is supported: "3[a2[c]]" decodes to "accaccacc"
Examples:
Input: encoded = '3[a]2[bc]'
Output: 'aaabcbc'
Explanation: '3[a]' decodes to 'aaa' and '2[bc]' decodes to 'bcbc'
Input: encoded = '3[a2[c]]'
Output: 'accaccacc'
Explanation: Inner '2[c]' decodes to 'cc', making 'acc', then '3[acc]' gives 'accaccacc'
Input: encoded = '2[abc]3[cd]ef'
Output: 'abcabccdcdcdef'
Explanation: '2[abc]' gives 'abcabc', '3[cd]' gives 'cdcdcd', then 'ef' is appended
Input: encoded = 'abc'
Output: 'abc'
Explanation: No brackets, return as is
Input: encoded = null
Output: null
Explanation: Null input returns null
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay July 2026
Contest runs July 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.