Problem 134841 · medium · Phase 01 Prerequisites & Setup

Tidy Up the Spaces

strings · strip · split · join

Copy-pasted text often carries stray spaces. Given s, return it with all spaces at the start and end removed and every run of one or more spaces between words reduced to a single space.

Examples

Input:  s = "  hello    big   world  "
Output: "hello big world"

Input:  s = "already tidy"
Output: "already tidy"

Constraints

  • 0 <= len(s) <= 5000
  • s may contain letters, digits, spaces and punctuation (no tabs or newlines)

Goals

  • Remove leading and trailing whitespace
  • Collapse repeated inner spaces to one
  • Combine `split` and `join`
Starting Python…