Count of lines required to write the given String | JAVA implementation of the approach ; 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
class GFG { static int [ ] numberOfLines ( String S , int [ ] widths ) { if ( S . isEmpty ( ) ) return new int [ ] { 0 , 0 } ; int lines = 1 , width = 0 ; for ( char character : S . toCharArray ( ) ) { int w = widths [ character - ' a ' ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return new int [ ] { lines , width } ; } public static void main ( String [ ] args ) { String S = " bbbcccdddaa " ; int 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 } ; int [ ] ans = numberOfLines ( S , widths ) ; System . out . print ( ans [ 0 ] + " ▁ " + ans [ 1 ] + "NEW_LINE"); } }