FastPrepCollapse Extra Spaces In Place

Collapse Extra Spaces In Place

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Remove leading and trailing ASCII spaces from text and replace every nonempty internal run of spaces with one space. Return the normalized string.

Function

collapseSpaces(text: String) → String

Examples

Example 1

text = "hello   world  there"return = "hello world there"

Each internal run collapses to one space.

Constraints

  • 0 <= text.length <= 10^5.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String collapseSpaces(String text) {
  // Write your code here.
}
text"hello world there"
expected"hello world there"
Checking account…