# 1592. Rearrange Spaces Between Words

![](/files/-MlSnm1eV4aWoPv4bqb4)

![](/files/-MlSnpNn-zX3whf_XtTH)

needs to be careful about the only **one word case, words.size()-1 would cause  divide by zero**

```java
// cal newSpace length, left space length, be careful about 1 
        int newSpace = space;
        int leftSpace = space;
        if (words.size() != 1) {
            newSpace = space/(words.size()-1);
            leftSpace = space%(words.size()-1);
        }
```

time: O(n)

space: O(n), result of text

```java
class Solution {
    public String reorderSpaces(String text) {
        int space = 0;
        int wordCount = 0;
        int totalLen = text.length();
        List<String> words = new ArrayList<>();
        
        
        int i = 0;
        while (i < totalLen) {
            // cal space count
            if (text.charAt(i) == ' ') { 
                space++;
                i++;
            } else { // record words
                int start = i;
                while(i < text.length() && text.charAt(i) != ' ') {
                    i++;
                }
                int end = i;
                words.add(text.substring(start, end));
            }
        }

        // cal newSpace length, left space length, be careful about 1 
        int newSpace = space;
        int leftSpace = space;
        if (words.size() != 1) {
            newSpace = space/(words.size()-1);
            leftSpace = space%(words.size()-1);
        }
        
        // build new space empty string
        StringBuilder newSpaceStr = new StringBuilder();
        for (int left = 0; left < newSpace; left++) {
            newSpaceStr.append(" ");
        }
        
        // compose new text
        StringBuilder result = new StringBuilder();
        for (int w = 0; w < words.size(); w++) {
            String word = words.get(w);
            result.append(word);
            if (w != words.size()-1) {
                result.append(newSpaceStr.toString());
            }
        }

        // add left space
        for (int left = 0; left < leftSpace; left++) {
            result.append(" ");
        }
        return result.toString();
    }
}
/*
count space
count word count
space/word count-1 = new space
space - new space
space - new space
left put in the tail
*/
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/string/1592.-rearrange-spaces-between-words.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
