FastPrepDecode String
Problems/Decode String

Decode String

Google logoGoogle● MediumFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The interview report identifies a variant of this nested decoding task but omits the variant details and bounds. The judged core task matches the visible source at about 92%.

Given a valid encoded string s, return its fully decoded form.

The encoding rule is k[encoded_string], meaning that the content inside the brackets is repeated exactly k times. Encoded groups may be nested, and adjacent literal or encoded groups are concatenated.

For this exercise, assume repetition counts are positive decimal integers and literal characters are lowercase English letters.

Function

decodeString(s: String) → String

Examples

Example 1

s = "3[a2[c]]"return = "accaccacc"

The inner group 2[c] becomes cc, so the outer group is 3[acc].

Example 2

s = "2[abc]3[cd]ef"return = "abcabccdcdcdef"

Decode the two repeated groups independently, then append the literal suffix ef.

Constraints

  • 1 <= s.length <= 10^5
  • s is a valid encoding with balanced brackets.
  • Every repetition count is in [1, 300].
  • Literal characters are lowercase English letters.
  • The decoded output length is at most 10^5.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String decodeString(String s) {
        // Write your code here.
    }
}
Checking account…