Given a
rows x cols
screen and a sentence represented by a list of words, find how many times the given sentence can be fitted on the screen.
Note:
- A word cannot be split into two lines.
- The order of words in the sentence must remain unchanged.
- Two consecutive words in a line must be separated by a single space.
- Total words in the sentence won't exceed 100.
- Length of each word won't exceed 10.
- 1 ≤ rows, cols ≤ 20,000.
Example 1:
Example 2:
Example 3:
A naive approach is to go through the screen and put each word in it. Whenever we find a word doesn't fit to the left cells, we switch to the next line. This approach is slow. Here is a way to optimize:
We first concatenate all strings together and add spaces between words. This string is the actual length we need to fit the string. Now we have a pos variable that initializes to 0, this is the position in the All string. Now for each row, we add cols to pos, this is the position in the All string that we can fit in the row. If the pos in the All string is pointing to a white space, we know we have just fitted (at least partial) strings in it. pos increment by 1. If its a letter, we know its in the middle of the word, so we need to decrease the pos to the start of the word. Here using pos % len indicates if pos is larger than len, it will go back to a position in the string.
public int wordsTyping(String[] sentence, int rows, int cols) { String all = ""; for (String s : sentence) { all += s + " "; } int pos = 0; int len = all.length(); for (int i = 0; i < rows; i++) { pos += cols; if (all.charAt(pos % len) == ' ') { pos++; } else { while (pos > 0 && all.charAt((pos - 1) % len) != ' ') { pos--; } } } return pos / len; }
public static int nFits(int rows, int cols, String[] words) {
ReplyDeleteint nwords = words.length;
int times=0;
int len, curr=0;
while(rows > 0) {
len = -1;
while(len + 1 + words[curr].length() <=cols) {
len += words[curr].length() + 1;
if(curr==nwords-1)
times++;
curr = (curr+1) % nwords;
}
rows--;
}
return times;
}
Hi Shirley,
ReplyDeleteWhat is the reason for "pos-1" at line 13?
Thanks in advance.