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 ordinary space, every run of ordinary spaces reduced to one, and no ordinary space at either end; preserve other whitespace characters unchanged.

Examples

Example 1

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

Output: "( a ( b ) c )"

Example 2

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

Output: "hello ( world )"

Rebuild it in the studio

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Streams & I/O problems