Count of lines required to write the given String | Function to return the number of lines required ; If String is empty ; Initialize lines and width ; Iterate through S ; Return lines and width used ; Driver Code ; Function call to print required answer
function numberOfLines ( S , widths ) { if ( S . length == 0 ) return [ 0 , 0 ] ; let lines = 1 , width = 0 ; for ( let character of S . split ( " " ) ) { let w = widths [ character . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return [ lines , width ] ; } let S = " " ; let widths = [ 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] ; let ans = numberOfLines ( S , widths ) ; document . write ( ans [ 0 ] + " " + ans [ 1 ] + " " ) ;