# Decode My String (Brackets) [duplicate]

Taken from StackOverflow Question

Challenge

Given an encoded string in format n[s] where n is the amount of times s will be repeated, your task is to output the corresponding decoded string.

• n will always be a positive number
• s can not contain empty spaces
• strings only contains letters from the alphabet
• s length can be >= 1
• encoded format is number [ string ]
• string can have nested encoded string number [ number [ string ] ]
• return the same string if not encoded

Examples

2[a]                 => aa
3[2[a]]              => aaaaaa
2[b3[a]]             => baaabaaa
3[a]5[r3[c]2[b]]a    => aaarcccbbrcccbbrcccbbrcccbbrcccbba
a                    => a  // Not Encoded
3                    => 3  // Not Encoded
3a3[b]               => 3abbb
10[b20[a]]           => baaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaa
3[hello]             => hellohellohello


Other test cases

a[a] => a[a]
a1[a] => aa
3[] =>    // Nothing because are 3 empty string
2[2][a] => 2[2][a]


This is my own submission.

f=i=>(r=/(\d+)$([a-z]*)$/g).test(l=i.replace(r,(m,n,s)=>s.repeat(n)))?f(l):l

console.log(f("2[a]"))
console.log(f("3[2[a]]"))
console.log(f("2[b3[a]]"))
console.log(f("3[a]5[r3[c]2[b]]a"))
console.log(f("a"))
console.log(f("3"))
console.log(f("3a3[b]"))
console.log(f("10[b20[a]]"))
console.log(f("3[hello]"))

If more test cases needed please tell me.

• Usually, someone who suggests a challenge, first tries putting it on the sandbox and doesn't answer it before a few days... – Olivier Grégoire Jun 11 '18 at 18:21
• Numbers can apparently be part of the strings, so what about: 12[a]? aaaaaaaaaaaa or 1aa? And why? – Stewie Griffin Jun 11 '18 at 18:25
• Why is 2[2][a] returned as unmodified string? Shouldn't it go 2[2][a] -> 22[a] -> aaaaaaaaaaaaaaaaaaaaaa? Or can string only contain lower case alphabet? – JungHwan Min Jun 11 '18 at 18:34
• This looks mostly good, but the 3[] test case is unclear to me: if the length of s is guaranteed to be at least 1, why is there a test case that has an s of length 0? – ETHproductions Jun 12 '18 at 3:03
• Very similar – ETHproductions Jun 12 '18 at 6:16

\d+$\pL*] ∊(⍎⍵M∩⎕D)⍴⊂⍵M∩⎕A  Try it online! Find: \d+\[\pL*] find digit(s) followed by bracketed Letters Replace with: ⎕A uppercase Alphabet ⍵M∩ intersection of Match and that ⊂ enclose (to treat as a whole) ()⍴reshape to length: ⎕DDigits ⍵M∩ intersection of Match and that ⍎ execute (converts text to number) ∊ϵnlist (flatten) This is equivalent to the Dyalog APL function: '\d+\[\pL*]'⎕R{∊(⍎⍵.Match∩⎕D)⍴⊂⍵.Match∩⎕A}⍣≡  # Perl 6, 44 36 bytes {({S{(\d+)\[(<:L>*)$}=$1 x$0}...*eq*).tail}


Test it

Nil while s{(\d+)$(<:L>*)$}=$1 x$0


Test it (with -p)

## Expanded:

Nil                   # silence warning by using Nil rather than 0

while                 # do the above while the following is truish

s {                 # string replace (implicitly on $_) ( \d+ ) # capture a series of digits into$0
$( <:L>* ) # capture letters$

} = $1 x$0         # replace it with the string repeated by the number


# Retina, 24 bytes

+(\d+)\[(\p{L}*)]
$1*$2


Try it online!

• Fails on "3[]" and "aaaaaaaaaaaaaaaaaaaaaa". (Should give "" and "2[2][a]".) Replacing \w+ with \p{L}* fixes these. – Adám Jun 11 '18 at 19:03
• I think you can remove the \ before ]. – Adám Jun 11 '18 at 19:09
• I don't know retina, but is changing \p{L} to \w valid? – tsh Jun 12 '18 at 6:35
• @tsh That would be invalid for 2[2][a] – Mr. Xcoder Jun 12 '18 at 6:36
• @tsh I had that before (when that test case didn't exist) and as Mr. Xcoder correctly points out, that test case invalidates it... – Dom Hastings Jun 12 '18 at 7:11

## Perl 5, 36 bytes

0while s/(\d+)\[([a-z]*)]/$2x$1/e


# Japt v2.0a0, 19 bytes

e/(\d+).(\l+)]/@ZpY


Try it

• "[11a]" produces "[1a" – recursive Jun 12 '18 at 5:19
• @recursive, according to the spec, that wouldn't be valid input; square brackets can only contain letters. – Shaggy Jun 12 '18 at 9:59
• "2[2][a]" is given as a test case. I interpret that to mean that the pattern should apply iff the square braces only contain letters. – recursive Jun 12 '18 at 13:14

# Stax, 21 bytes

è/g1┘+α╣m?╣t╥ó⌂ß─"H║à


Run and debug it

# ReRegex, 83 bytes

#import base
(\d+)\[([a-z]+)]/Au<$1>,$2,B/_,([a-z]+),/,$1,$1/A,.*?,(.*?)B/\$1/#input


ReRegex is a language designed around regex. Sadly, it doesn't really understand numbers. So repeating the string is the hardest part of this.

Try it online!

# Red, 158 bytes

func[s][b: charset[not"[]"]d: charset"0123456789"until[r: on
parse s[to remove[copy n any d"["copy t any b"]"](insert/dup
c: copy""t do n r: off)insert c]r]s]


Try it online!

f: func [ s ] [
b: charset [ not "[]" ]
d: charset "0123456789"
until [
r: on
parse s [
to remove [ copy n some d "[" copy t any b "]" ]
( insert/dup c: copy "" t do n
r: off )
insert c ]
r
]
s
]
`