# 418 Sentence Screen Fitting

T: O(rows\*max of word length)

S: O(all words total length)

```java
class Solution {
    public int wordsTyping(String[] sentence, int rows, int cols) {
        StringBuilder sb = new StringBuilder();
        for (String s : sentence) {
            sb.append(s + " ");
        }
        String s = sb.toString();
        int len = s.length();
        
        int cursor = 0;
        for (int i = 0; i < rows; i++) {
            cursor += cols;
            
            if (s.charAt(cursor%len) == ' ') {
                cursor++;
            } else {
                while (cursor >= 0 && s.charAt(cursor%len) != ' ') {
                    cursor--;
                }
                cursor++;
            }
        }
        return cursor/len;
    }
}
```

## more concise version

```java
class Solution {
    public int wordsTyping(String[] sentence, int rows, int cols) {
        StringBuilder sb = new StringBuilder();
        for (String s : sentence) {
            // if (s.length() > cols) { // notice this case, so if we only make idx min is 0 (idx >= 0)
            //     return 0;
            // }
            sb.append(s + " ");
        }
        String sentenceStr = sb.toString();
        int len = sentenceStr.length();
        
        int idx = 0;
        for (int i = 0; i < rows; i++) {
            idx += cols;
            while (idx >= 0 && sentenceStr.charAt(idx%len) != ' ') { // make idx min is 0, in the last idx/len = 0
                idx--;
            }
            idx++;
        }
        return idx/len;
    }
}
```

#### 最簡潔的寫法,  use String.join(" ", str)

```java
class Solution {
    public int wordsTyping(String[] sentence, int rows, int cols) {
        String s = String.join(" ", sentence) + " ";
        int len = s.length();
        
        int idx = 0;
        for (int i = 0; i < rows; i++) {
            idx += cols;
            while (idx >= 0 && s.charAt(idx%len) != ' ') { // make idx min is 0, in the last idx/len = 0
                idx--;
            }
            idx++;
        }
        return idx/len;
    }
}
```

這題的這個寫法的想法, 相當 tricky

ex:&#x20;

Input: sentence = \["a", "bcd", "e"], rows = 3, cols = 6

概念轉換成像是在一個無窮的多個 sentence 組成的長 sentences 上以 row 的長度來做斷句

<figure><img src="/files/O8s3WAg3AXtkPMsV5oSJ" alt=""><figcaption></figcaption></figure>


---

# 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/418-sentence-screen-fitting.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.
