418 Sentence Screen Fitting
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
ๆ็ฐกๆฝ็ๅฏซๆณ, use String.join(" ", str)

Last updated