Class Solution

java.lang.Object
g0301_0400.s0394_decode_string.Solution

public class Solution extends Object
394 - Decode String.<p>Medium</p> <p>Given an encoded string, return its decoded string.</p> <p>The encoding rule is: <code>k[encoded_string]</code>, where the <code>encoded_string</code> inside the square brackets is being repeated exactly <code>k</code> times. Note that <code>k</code> is guaranteed to be a positive integer.</p> <p>You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.</p> <p>Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, <code>k</code>. For example, there won&rsquo;t be input like <code>3a</code> or <code>2[4]</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;3[a]2[bc]&rdquo;</p> <p><strong>Output:</strong> &ldquo;aaabcbc&rdquo;</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;3[a2[c]]&rdquo;</p> <p><strong>Output:</strong> &ldquo;accaccacc&rdquo;</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;2[abc]3[cd]ef&rdquo;</p> <p><strong>Output:</strong> &ldquo;abcabccdcdcdef&rdquo;</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> s = &ldquo;abc3[cd]xyz&rdquo;</p> <p><strong>Output:</strong> &ldquo;abccdcdcdxyz&rdquo;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 30</code></li> <li><code>s</code> consists of lowercase English letters, digits, and square brackets <code>'[]'</code>.</li> <li><code>s</code> is guaranteed to be <strong>a valid</strong> input.</li> <li>All the integers in <code>s</code> are in the range <code>[1, 300]</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details