#Problem

#⭐️

Number of Lines To Write String

#Solution

Easy

#Code

#Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
private static final int LIMIT = 100;
public int[] numberOfLines(int[] widths, String S) {
final int[] res = new int[2];
if (S.isEmpty()) return res;
int lines = 1;
int count = 0;
final char[] chs = S.toCharArray();
for (final char c : chs) {
if (count + widths[c - 'a'] > LIMIT) {
lines++;
count = widths[c - 'a'];
} else {
count += widths[c - 'a'];
}
}
res[0] = lines;
res[1] = count;
return res;
}
}

Comments