Count of lines required to write the given String | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > numberOfLines ( string S , int * widths ) { if ( S . empty ( ) ) return { 0 , 0 } ; int lines = 1 , width = 0 ; for ( auto character : S ) { int w = widths [ character - ' a ' ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return { lines , width } ; } int main ( ) { 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 } ; pair < int , int > ans = numberOfLines ( S , widths ) ; cout << ans . first << " ▁ " << ans . second << endl ; return 0 ; }