Sentence Screen Fitting

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.
    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.

Example 3:

Input:
rows = 4, cols = 5, sentence = ["I", "had", "apple", "pie"]

Output: 
1

Explanation:
I-had
apple
pie-I
had--

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

思路
假設(shè)string是無(wú)限循環(huán)下去的,需要記錄所有row走完會(huì)move到哪里。用start指針的值記錄一共在screen上有多少字符。最后通過start / sentence length即可知道一共能打印下多少個(gè)sentence。

具體包含以下步驟:

  1. 先把sentence連起來(lái),然后攤平。例如:[“abc”, “de”, “f”],連成sentence是”abc de f “,設(shè)想這個(gè)sentence無(wú)限循環(huán),就是”abc de f abc de f abc de f abc…”
  2. 用start pointer記錄每一行的start,每多一個(gè)row,把pointer往后移動(dòng)cols個(gè)。
  3. 移動(dòng)pointer時(shí)兩種情況需要處理一下:
    • 一行的開頭剛好是space,start指針直接+1,即remove一個(gè)space
    • 一行的開頭剛好是一個(gè)word的中間,start需要退回到該word的開頭,即在上一行增加一些space。
class Solution {
    public int wordsTyping(String[] sentence, int rows, int cols) {
        String allSentence = "";
            
        //1. reorganized the sentence using " "
        for (String word : sentence) {
            allSentence += word + " ";
        }
        int len = allSentence.length();
        
        //2. calculate the start pointer
        int start = 0;
        
        for (int i = 0; i < rows; i++) {
            start = start + cols;
            if (allSentence.charAt(start % len) == ' ') {
                start++;
            } else {
                while (start > 0 && allSentence.charAt((start - 1) % len) != ' ') {
                    start--;
                }
            }
        }
        
        return start / len;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 四.苦惱 蘭子起個(gè)大早,想去梅子家看看。可等待她的是鐵將軍把門,敲門,也沒有人應(yīng)答。蘭子一臉納悶,人都到哪去了,這...
    上善若水1980閱讀 353評(píng)論 0 0
  • 最近,氣溫一降再降,雖身在南方,相比北方再冷也不過如此,但南方的冷不僅僅是溫度計(jì)上的個(gè)位數(shù),還帶一點(diǎn)點(diǎn)潮濕,這樣的...
    亦沁閱讀 195評(píng)論 0 0
  • 文章談到的最近熱播的《我的前半生》羅子君被出軌那橋段吧,作為好友的唐晶好言相勸反而招到羅子君的抱怨。質(zhì)疑“到底...
    控心_ebed閱讀 310評(píng)論 0 2
  • 那躺著一具尸體 不 是一個(gè)人 我將他叫醒 停頓片刻 他牽強(qiáng)的笑 而后顫抖走著 不知是哪個(gè)方向 ...
    Breeze子凡閱讀 284評(píng)論 0 0

友情鏈接更多精彩內(nèi)容