418. Sentence Screen Fitting

https://leetcode.com/problems/sentence-screen-fitting/discuss/90845/21ms-18-lines-Java-solution


https://www.youtube.com/watch?v=ZeLrhECnlF4




https://www.dotnetperls.com/join-java

Given a rows x cols screen and a sentence represented by a list of non-empty words, find how many times the given sentence can be fitted on the screen.
Note:
1. A word cannot be split into two lines.
2. The order of words in the sentence must remain unchanged.
3. Two consecutive words in a line must be separated by a single space.
4. Total words in the sentence won't exceed 100.
5. Length of each word is greater than 0 and won't exceed 10.
6. 1 ≤ rows, cols ≤ 20,000.

Example 1:
Input:
rows = 2, cols = 8, sentence = ["hello", "world"]

Output: 
1

Explanation:
hello---
world---

The character '-' signifies an empty space on the screen.

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

Output: 
2

Explanation:
a-bcd- 
e-a---
bcd-e-

The character '-' signifies an empty space on the screen.




Java program that uses String.join method

public class Program {
    public static void main(String[] args) {

        // Create String array of three elements.
        String[] values = { "bird", "cat", "wildebeest" };

        // Join the elements together.
        String result = String.join("...", values);
        System.out.println(result);
    }
}

Output

bird...cat...wildebeest

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9927003.html