FastPrepDecode String

Decode String

Compass International Holdings logoCompass International Holdings● MediumFULLTIMEPHONE SCREEN

Problem statement

You are given an encoded string s. Return the decoded string.

The encoding is k[text], which means text repeated exactly k times. Groups can be nested, so text can contain more groups. Letters and groups that sit next to each other are joined in the order they appear.

Digits are used only for counts, so the decoded string contains only letters.

Function

decodeString(s: String) → String

Examples

Example 1

s = "3[a]2[bc]"return = "aaabcbc"

3[a] becomes aaa and 2[bc] becomes bcbc. Joined, they give aaabcbc.

Example 2

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

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

Example 3

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

The two groups give abcabc and cdcdcd. The letters ef stay at the end.

Example 4

s = "ab12[x]y"return = "abxxxxxxxxxxxxy"

The count 12 has two digits, so x repeats twelve times between ab and y.

Constraints

  • 1 <= s.length <= 1000
  • s contains only lowercase English letters, digits and square brackets.
  • s is a valid encoding: brackets are balanced, every [ comes right after a count, every count is followed by [, and every group has at least one character inside.
  • Every count is a whole number from 1 to 300 with no leading zeros.
  • The decoded string has at most 30000 letters.

More Compass International Holdings problems

See Compass International Holdings hiring insights
Saved
public String decodeString(String s) {
    // Write your code here.
}
Checking account…