Number of Lines To Write String LeetCode solution in JavaScript
1var numberOfLines = function (widths, s) {2let pixel = 100,3line = 1;4for (let i = 0; i < s.length; i++) {5if (pixel >= widths[s[i].charCodeAt() - 97]) {6pixel -= widths[s[i].charCodeAt() - 97];7} else {8// this word should be written in NEXT line, so it CANNOT count.9i--;10line++;11pixel = 100;12}13}14// 100-pixel = space used in this line.15return [line, 100 - pixel];16};