给你一个字符串 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 中至少包含一个单词
var reorderSpaces = function(text) {
const n = text.length, words = []
let cntSpace = 0, cntWord = 0
for (let i = 0; i < n; i++) {
if (text[i] === ' ') cntSpace++
else if (i == 0 || text[i - 1] === ' ') words[cntWord++] = [text[i]]
else words[cntWord - 1] += text[i]
}
if (cntWord === 1) return words[0] + ' '.repeat(cntSpace)
return words.join(' '.repeat(cntSpace / (cntWord - 1) | 0)) + ' '.repeat(cntSpace % (cntWord - 1))
};
function reorderSpaces(text: string): string {
const n = text.length, words = []
let cntSpace = 0, cntWord = 0
for (let i = 0; i < n; i++) {
if (text[i] === ' ') cntSpace++
else if (i === 0 || text[i - 1] === ' ') words[cntWord++] = text[i]
else words[cntWord - 1] += text[i]
}
if (cntWord === 1) return words[0] + ' '.repeat(cntSpace)
return words.join(' '.repeat(cntSpace / (cntWord - 1) | 0)) + ' '.repeat(cntSpace % (cntWord - 1))
};
class Solution {
function reorderSpaces($text) {
$n = strlen($text);
$words = [];
$cntWord = 0;
$cntSpace = 0;
for ($i = 0; $i < $n; $i++) {
if ($text[$i] === ' ') $cntSpace++;
else if ($i === 0 || $text[$i - 1] === ' ') $words[$cntWord++] = $text[$i];
else $words[$cntWord - 1] .= $text[$i];
}
if ($cntWord === 1) return $words[0] . str_repeat(' ', $cntSpace);
return implode(str_repeat(' ', intval($cntSpace / ($cntWord - 1))), $words) .
str_repeat(' ', $cntSpace % ($cntWord - 1));
}
}
func reorderSpaces(text string) string {
cntSpace, words, sb := 0, []string{}, strings.Builder{}
for i, c := range text {
if c == ' ' {
cntSpace++
if i > 0 && text[i - 1] != ' ' {
words = append(words, sb.String())
sb.Reset()
}
} else {
sb.WriteByte(text[i])
}
}
if sb.Len() > 0 {
words = append(words, sb.String())
}
cntWord := len(words)
if cntWord == 1 {
return words[0] + strings.Repeat(" ", cntSpace)
}
return strings.Join(words, strings.Repeat(" ", cntSpace / (cntWord - 1) | 0)) +
strings.Repeat(" ", cntSpace % (cntWord - 1))
}
class Solution {
public String reorderSpaces(String text) { // + 拼接字符串
int n = text.length(), cntSpace = 0;
List<String> words = new ArrayList<String>();
for (int i = 0; i < n; i++) {
if (text.charAt(i) == ' ') cntSpace++;
else if (i == 0 || text.charAt(i - 1) == ' ') words.add("" + text.charAt(i));
else words.set(words.size() - 1, words.get(words.size() - 1) + text.charAt(i));
}
int cntWord = words.size();
if (cntWord == 1) return words.get(0) + " ".repeat(cntSpace);
return String.join(" ".repeat(cntSpace / (cntWord - 1) | 0), words) + " ".repeat(cntSpace % (cntWord - 1));
}
}
class Solution {
public String reorderSpaces(String text) { // StringBuilder 拼接字符串
int n = text.length(), cntSpace = 0;
List<String> words = new ArrayList<String>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
if (text.charAt(i) == ' ') {
cntSpace++;
if (i > 0 && text.charAt(i - 1) != ' ') {
words.add(sb.toString());
sb.delete(0, sb.length());
}
} else {
sb.append(text.charAt(i));
}
}
if (sb.length() > 0) words.add(sb.toString());
int cntWord = words.size();
if (cntWord == 1) return words.get(0) + " ".repeat(cntSpace);
return String.join(" ".repeat(cntSpace / (cntWord - 1) | 0), words) + " ".repeat(cntSpace % (cntWord - 1));
}
}
public class Solution {
public string ReorderSpaces(string text) { // StringBuilder
IList<string> words = new List<string>();
StringBuilder sb = new StringBuilder();
int n = text.Length, cntSpace = 0;
for (int i = 0; i < n; i++) {
if (text[i] == ' ') {
cntSpace++;
if (i > 0 && text[i - 1] != ' ') {
words.Add(sb.ToString());
sb.Clear();
}
} else {
sb.Append(text[i]);
}
}
if (sb.Length > 0) words.Add(sb.ToString());
int cntWord = words.Count;
if (cntWord == 1) return words[0] + new string(' ', cntSpace);
return string.Join(new string(' ', cntSpace / (cntWord - 1) | 0), words) +
new string(' ', cntSpace % (cntWord - 1));
}
}
static inline char* join(char* s, char** a, int aSize, int aCnt) {
int m = strlen(s), n = (aCnt - 1) * m + aSize;
char* r = malloc(sizeof(char) * (n + 1));
int pos = 0;
for (int i = 0; i < aCnt; i++) {
int wn = strlen(a[i]);
for (int j = 0; j < wn; j++) {
r[pos++] = a[i][j];
}
if (i < aCnt - 1) {
for (int k = 0; k < m; k++) {
r[pos++] = s[k];
}
}
}
r[pos] = '\0';
return r;
}
static inline char* repeat(char s, int n) {
char* r = malloc(sizeof(char) * (n + 1));
for (int i = 0; i < n; i++) r[i] = s;
r[n] = '\0';
return r;
}
char * reorderSpaces(char * text){
int n = strlen(text), cntSpace = 0, cntWord = 0;
char** words = malloc(sizeof(char*) * n);
char* sb = malloc(sizeof(char) * (n + 1));
int p = 0;
for (int i = 0; i < n; i++) {
if (text[i] == ' ') {
cntSpace++;
if (i > 0 && text[i - 1] != ' ') {
sb[p] = '\0';
words[cntWord] = malloc(sizeof(char) * (p + 1));
memcpy(words[cntWord], sb, p + 1);
cntWord++;
p = 0;
}
} else {
sb[p++] = text[i];
}
}
if (p) {
sb[p] = '\0';
words[cntWord] = malloc(sizeof(char) * (p + 1));
memcpy(words[cntWord], sb, p + 1);
cntWord++;
}
char* r = malloc(sizeof(char) * (n + 1));
if (cntWord == 1) sprintf(r, "%s%s", words[0], repeat(' ', cntSpace));
else sprintf(r, "%s%s", join(repeat(' ', cntSpace / (cntWord - 1) | 0), words, n, cntWord),
repeat(' ', cntSpace % (cntWord - 1)));
return r;
}
class Solution {
private:
string join(string d, vector<string> &s) {
return accumulate(s.begin(), s.end(), string(), [&d](string &prev, string &cur) -> string {
return prev.empty() ? next : prev + d + cur;
});
}
public:
string reorderSpaces(string text) {
int n = text.size(), cntSpace = 0;
vector<string> words;
for (int i = 0; i < n; i++) {
if (text[i] == ' ') cntSpace++;
else if (i == 0 || text[i - 1] == ' ') words.emplace_back(string(1, text[i]));
else words.back() += text[i];
}
int cntWord = words.size();
if (cntWord == 1) return words[0] + string(cntSpace, ' ');
return join(string(cntSpace / (cntWord - 1) | 0, ' '), words) +
string(cntSpace % (cntWord - 1), ' ');
}
};
class Solution {
private:
string join(string d, vector<string> &s) {
return accumulate(s.begin(), s.end(), string(), [&d](string &prev, string &cur) -> string {
return prev.empty() ? cur : prev + d + cur;
});
}
public:
string reorderSpaces(string text) {
int n = text.size(), cntSpace = 0;
vector<string> words;
string sb;
for (int i = 0; i < n; i++) {
if (text[i] == ' ') {
cntSpace++;
if (i > 0 && text[i - 1] != ' ') {
words.emplace_back(sb);
sb = "";
}
} else {
sb += text[i];
}
}
if (sb != "") words.emplace_back(sb);
int cntWord = words.size();
if (cntWord == 1) return words[0] + string(cntSpace, ' ');
return join(string(cntSpace / (cntWord - 1) | 0, ' '), words) +
string(cntSpace % (cntWord - 1), ' ');
}
};
class Solution:
def reorderSpaces(self, text: str) -> str:
words, cntSpace = [], 0
for i, c in enumerate(text):
if c == ' ': cntSpace += 1
elif i == 0 or text[i - 1] == ' ': words.append(c)
else: words[-1] += c
cntWord = len(words)
if cntWord == 1: return words[0] + ' ' * cntSpace
return (' ' * (cntSpace // (cntWord - 1))).join(words) + ' ' * (cntSpace % (cntWord - 1))
class Solution:
def reorderSpaces(self, text: str) -> str:
words, cntSpace, sb = [], 0, ''
for i, c in enumerate(text):
if c == ' ':
cntSpace += 1
if i > 0 and text[i - 1] != ' ':
words.append(sb)
sb = ''
else:
sb += c
if sb: words.append(sb)
cntWord = len(words)
if cntWord == 1: return words[0] + ' ' * cntSpace
return (' ' * (cntSpace // (cntWord - 1))).join(words) + ' ' * (cntSpace % (cntWord - 1))
var reorderSpaces = function(text) {
const a = text.split(''), n = text.length
let cntWord = 0, cntSpace = n
for (let pw = ps = n - 1, b = false; pw >= 0; pw--) {
if (a[pw] !== ' ') {
swap(a, pw, ps--)
if (b === false) cntWord++, b = true
cntSpace--
} else if (b === true) {
b = false
ps--
}
}
const avg = cntSpace / Math.max(1, cntWord - 1) | 0
for (let pw = ps = 0, b = false; pw < n; pw++) {
if (a[pw] !== ' ') {
swap(a, pw, ps++)
b = true
} else if (b === true) {
b = false
ps += avg
}
}
return a.join('')
};
const swap = (nums, a, b) => {
if (a === b) return
const t = nums[a]
nums[a] = nums[b]
nums[b] = t
}
function reorderSpaces(text: string): string {
const a = text.split(''), n = a.length
let cntSpace = n, cntWord = 0
for (let pw = n - 1, ps = n - 1, b = false; pw >= 0; pw--) {
if (a[pw] !== ' ') {
swap(a, pw, ps--)
if (b === false) b= true, cntWord++
cntSpace--
} else if (b) {
b = false
ps--
}
}
const avg = cntSpace / Math.max(1, cntWord - 1) | 0
for (let pw = 0, ps = 0, b = false; pw < n; pw++) {
if (a[pw] !== ' ') {
swap(a, pw, ps++)
b = true
} else if (b) {
b = false
ps += avg
}
}
return a.join('')
};
function swap(nums, a, b) {
if (a === b) return
const t = nums[a]
nums[a] = nums[b]
nums[b] = t
}
class Solution {
function reorderSpaces($text) {
$n = strlen($text);
$a = str_split($text);
$cntSpace = $n;
$cntWord = 0;
for ($pw = $ps = $n - 1, $b = false; $pw >= 0; $pw--) {
if ($a[$pw] !== ' ') {
$this->swap($a, $pw, $ps--);
if ($b === false) {
$b = true;
$cntWord++;
}
$cntSpace--;
} else if ($b) {
$b = false;
$ps--;
}
}
$avg = intval($cntSpace / max(1, $cntWord - 1));
for ($pw = $ps = 0, $b = false; $pw < $n ;$pw++) {
if ($a[$pw] !== ' ') {
$this->swap($a, $pw, $ps++);
$b = true;
} else if ($b) {
$b = false;
$ps += $avg;
}
}
return implode('', $a);
}
function swap(&$nums, $a, $b) {
if ($a === $b) return;
list($nums[$a], $nums[$b]) = array($nums[$b], $nums[$a]);
}
}
func reorderSpaces(text string) string {
n := len(text)
cntSpace, cntWord, a := n, 0, strings.Split(text, "")
for pw, ps, b := n - 1, n - 1, false; pw >= 0; pw-- {
if a[pw] != " " {
swap(a, pw, ps)
ps--
if b == false {
b = true
cntWord++
}
cntSpace--
} else if (b) {
b = false
ps--
}
}
avg := cntSpace / max(1, cntWord - 1) | 0
for pw, ps, b := 0, 0, false; pw < n; pw++ {
if a[pw] != " " {
swap(a, pw, ps)
ps++
b = true
} else if (b) {
b = false
ps += avg
}
}
return strings.Join(a, "")
}
func swap(nums []string, a, b int) {
if a == b {
return
}
nums[a], nums[b] = nums[b], nums[a]
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
class Solution {
public String reorderSpaces(String text) {
int n = text.length(), cntSpace = n, cntWord = 0;
String[] a = text.split("");
for (int pw = n - 1, ps = n - 1, b = 0; pw >= 0; pw--) {
if (a[pw].equals(" ") == false) {
swap(a, pw, ps--);
if (b == 0) {
b = 1;
cntWord++;
}
cntSpace--;
} else if (b == 1) {
b = 0;
ps--;
}
}
int avg = cntSpace / Math.max(1, cntWord - 1);
for (int pw = 0, ps = 0, b = 0; pw < n; pw++) {
if (a[pw].equals(" ") == false) {
swap(a, pw, ps++);
b = 1;
} else if (b == 1) {
b = 0;
ps += avg;
}
}
return String.join("", a);
}
public void swap(String[] nums, int a, int b) {
if (a == b) return;
String t = nums[a];
nums[a] = nums[b];
nums[b] = t;
}
}
public class Solution {
public string ReorderSpaces(string text) {
int n = text.Length, cntWord = 0, cntSpace = n;
char[] a = text.ToCharArray();
for (int pw = n - 1, ps = n - 1, b = 0; pw >= 0; pw--) {
if (a[pw] != ' ') {
swap(a, pw, ps--);
if (b == 0) {
b = 1;
cntWord++;
}
cntSpace--;
} else if (b == 1) {
b = 0;
ps--;
}
}
int avg = cntSpace / Math.Max(1, cntWord - 1);
for (int pw = 0, ps = 0, b = 0; pw < n; pw++) {
if (a[pw] != ' ') {
swap(a, pw, ps++);
b = 1;
} else if (b == 1) {
b = 0;
ps += avg;
}
}
return new string(a);
}
public void swap(char[] nums, int a, int b) {
if (a == b) return;
char t = nums[a];
nums[a] = nums[b];
nums[b] = t;
}
}
#define MAX(a, b) (a > b ? a : b)
char * reorderSpaces(char * text){ // swap 值交换
int n = strlen(text), cntSpace = n, cntWord = 0;
for (int pw = n - 1, ps = n - 1, b = 0; pw >= 0; pw--) {
if (text[pw] != ' ') {
swap(text, pw, ps--);
if (b == 0) b = 1, cntWord++;
cntSpace--;
} else if (b == 1) {
b = 0;
ps--;
}
}
int avg = cntSpace / MAX(1, cntWord - 1);
for (int pw = 0, ps = 0, b = 0; pw < n; pw++) {
if (text[pw] != ' ') {
swap(text, pw, ps++);
b = 1;
} else if (b == 1) {
b = 0;
ps += avg;
}
}
return text;
}
void swap(char* nums, int a, int b) {
char t = nums[a];
nums[a] = nums[b];
nums[b] = t;
}
#define MAX(a, b) (a > b ? a : b)
char * reorderSpaces(char * text){ // swap 指针交换
int n = strlen(text), cntSpace = n, cntWord = 0;
for (int pw = n - 1, ps = n - 1, b = 0; pw >= 0; pw--) {
if (text[pw] != ' ') {
swap(text + pw, text + ps--);
if (b == 0) b = 1, cntWord++;
cntSpace--;
} else if (b == 1) {
b = 0;
ps--;
}
}
int avg = cntSpace / MAX(1, cntWord - 1);
for (int pw = 0, ps = 0, b = 0; pw < n; pw++) {
if (text[pw] != ' ') {
swap(text + pw, text + ps++);
b = 1;
} else if (b == 1) {
b = 0;
ps += avg;
}
}
return text;
}
void swap(char* a, char* b) {
char t = *a;
*a = *b;
*b = t;
}
class Solution {
public:
string reorderSpaces(string text) {
int n = text.size(), cntWord = 0, cntSpace = n;
for (int pw = n - 1, ps = n - 1, b = 0; pw >= 0; pw--) {
if (text[pw] != ' ') {
swap(text[pw], text[ps--]);
if (b == 0) b = 1, cntWord++;
cntSpace--;
} else if (b == 1) {
b = 0;
ps--;
}
}
int avg = cntSpace / max(1, cntWord - 1);
for (int pw = 0, ps = 0, b = 0; pw < n; pw++) {
if (text[pw] != ' ') {
swap(text[pw], text[ps++]);
b = 1;
} else if (b == 1) {
b = 0;
ps += avg;
}
}
return text;
}
};
class Solution:
def swap(self, nums: list, a: int, b: int):
nums[a], nums[b] = nums[b], nums[a]
def reorderSpaces(self, text: str) -> str:
n, a = len(text), list(text)
cntSpace, cntWord, ps, b = n, 0, n - 1, 0
for pw in range(n - 1, -1, -1):
if a[pw] != ' ':
self.swap(a, pw, ps)
ps, cntSpace = ps - 1, cntSpace - 1
if b == 0:
b = 1
cntWord += 1
elif b == 1:
ps, b = ps - 1, 0
avg = cntSpace // max(1, cntWord - 1)
ps, b = 0, 0
for pw in range(0, n):
if a[pw] != ' ':
self.swap(a, pw, ps)
ps, b = ps + 1, 1
elif b == 1:
ps, b = ps + avg, 0
return ''.join(a)