LeetCode 题解工作台

重新排列单词间的空格

给你一个字符串 text ,该字符串由若干被空格包围的单词组成。每个单词由一个或者多个小写英文字母组成,并且两个单词之间至少存在一个空格。题目测试用例保证 text 至少包含一个单词 。 请你重新排列空格,使每对相邻单词之间的空格数目都 相等 ,并尽可能 最大化 该数目。如果不能重新平均分配所有空格…

category

1

题型

code_blocks

6

代码语言

hub

3

相关题

当前训练重点

简单 · String-driven solution strategy

bolt

答案摘要

我们先统计字符串 中的空格数,记为 。将 按空格分割成字符串数组 。然后计算相邻字符串之间需要拼接的空格数,进行拼接。最后将剩余的空格拼接在末尾。 时间复杂度 ,空间复杂度 ,其中 表示字符串 的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 String-driven solution strategy 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串 text ,该字符串由若干被空格包围的单词组成。每个单词由一个或者多个小写英文字母组成,并且两个单词之间至少存在一个空格。题目测试用例保证 text 至少包含一个单词

请你重新排列空格,使每对相邻单词之间的空格数目都 相等 ,并尽可能 最大化 该数目。如果不能重新平均分配所有空格,请 将多余的空格放置在字符串末尾 ,这也意味着返回的字符串应当与原 text 字符串的长度相等。

返回 重新排列空格后的字符串

 

示例 1:

输入:text = "  this   is  a sentence "
输出:"this   is   a   sentence"
解释:总共有 9 个空格和 4 个单词。可以将 9 个空格平均分配到相邻单词之间,相邻单词间空格数为:9 / (4-1) = 3 个。

示例 2:

输入:text = " practice   makes   perfect"
输出:"practice   makes   perfect "
解释:总共有 7 个空格和 3 个单词。7 / (3-1) = 3 个空格加上 1 个多余的空格。多余的空格需要放在字符串的末尾。

示例 3:

输入:text = "hello   world"
输出:"hello   world"

示例 4:

输入:text = "  walks  udp package   into  bar a"
输出:"walks  udp  package  into  bar  a "

示例 5:

输入:text = "a"
输出:"a"

 

提示:

  • 1 <= text.length <= 100
  • text 由小写英文字母和 ' ' 组成
  • text 中至少包含一个单词
lightbulb

解题思路

方法一:字符串模拟

我们先统计字符串 text\textit{text} 中的空格数,记为 spaces\textit{spaces}。将 text\textit{text} 按空格分割成字符串数组 words\textit{words}。然后计算相邻字符串之间需要拼接的空格数,进行拼接。最后将剩余的空格拼接在末尾。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n),其中 nn 表示字符串 text\textit{text} 的长度。

1
2
3
4
5
6
7
8
9
class Solution:
    def reorderSpaces(self, text: str) -> str:
        spaces = text.count(" ")
        words = text.split()
        if len(words) == 1:
            return words[0] + " " * spaces
        cnt, mod = divmod(spaces, len(words) - 1)
        return (" " * cnt).join(words) + " " * mod
speed

复杂度分析

指标
时间complexity is O(n) to scan the string and count words and spaces. Space complexity is O(n) for storing words and building the output string.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Looking for efficient string parsing without extra passes over the text.

  • question_mark

    Expect candidate to handle cases with a single word where no spaces can be distributed between words.

  • question_mark

    Watch for correct placement of leftover spaces at the end to preserve original string length.

warning

常见陷阱

外企场景
  • error

    Forgetting to handle the single-word edge case, which requires all spaces at the end.

  • error

    Incorrectly dividing spaces when words are fewer than two, leading to division by zero.

  • error

    Appending leftover spaces incorrectly, breaking the original string length constraint.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Redistribute spaces but preserve punctuation attached to words.

  • arrow_right_alt

    Maximize spacing while also aligning words to the right side of the string.

  • arrow_right_alt

    Redistribute spaces evenly for multi-line strings where each line must maintain its original length.

help

常见问题

外企场景

重新排列单词间的空格题解:String-driven solution … | LeetCode #1592 简单