Easy · Streams & I/O

Add Spaces Around Parentheses in a String

Given a string s, return it with every '(' and ')' set apart from its neighbours by exactly one space, every run of spaces reduced to one space, and no space at either end.

Examples

Example 1

{
  "s": "(a(b)c)"
}

Output: "( a ( b ) c )"

Example 2

{
  "s": "hello(world)"
}

Output: "hello ( world )"

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Streams & I/O problems