# Count of lines required to write the given String

Given a string **str** and an integer array **width[]** where:

width[0] = width of character ‘a’

width[1] = width of character ‘b’

…

width[25] = width of character ‘z’

The task is to find the number of lines it’ll take to write the string **str** on a paper and the width of the last line upto which it is occupied.

**Note:** Width of a line is **10 units**.

**Examples:**

Input:str = “bbbcccdddaa”,

width[] = {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}

Output:(2, 8)

“bbbcccddd” will cover first line (9 * 1 = 9 units)

As ‘a’ has a width of 4 which cannot fit the remaining 1 unit in the first line.

It’ll have to be written in the second line.

So, next line will contain “aa” covering 4 * 2 = 8 units.

We need 1 full line and one line with width 8 units.

Input:str = “abcdefghijklmnopqrstuvwxyz”,

width[] = {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, 1}

Output:(3, 6)

All the characters have the same width of 1. To write all 26 characters,

We need 2 full lines and one line with width 6 units.

**Approach:** We will write each character in the string **str** one by one. As we write a character, we immediately update (lines, width) that keeps track of how many lines we have used till now and what is the length of the used space in the last line.

If the **width[char]** in **str** fits our current line, we will add it. Otherwise, we will start with a new line

Below is the implementation of the above approach:

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the number of lines required ` `def` `numberOfLines(S, widths): ` ` ` ` ` `# If string is empty ` ` ` `if` `(S ` `=` `=` `""): ` ` ` `return` `0` `, ` `0` ` ` ` ` `# Initialize lines and width ` ` ` `lines, width ` `=` `1` `, ` `0` ` ` ` ` `# Iterate through S ` ` ` `for` `c ` `in` `S: ` ` ` `w ` `=` `widths[` `ord` `(c) ` `-` `ord` `(` `'a'` `)] ` ` ` `width ` `+` `=` `w ` ` ` `if` `width > ` `10` `: ` ` ` `lines ` `+` `=` `1` ` ` `width ` `=` `w ` ` ` ` ` `# Return lines and width used ` ` ` `return` `lines, width ` ` ` ` ` `# Driver Code ` `S ` `=` `"bbbcccdddaa"` `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` `] ` ` ` `# Function call to print required answer ` `print` `(numberOfLines(S, Widths)) ` |

*chevron_right*

*filter_none*

**Output:**

(2, 8)

## Recommended Posts:

- Minimum number of given operations required to convert a string to another string
- Minimum changes required to make first string substring of second string
- Write a program to print all permutations of a given string
- Write a program to reverse an array or string
- Count of matchsticks required to represent the given number
- Count the number of carry operations required to add two numbers
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Minimum rotations required to get the same String | Set-2
- Minimum rotations required to get the same string
- Minimum changes required such that the string satisfies the given condition
- Operations required to make the string empty
- Minimum operations required to convert a binary string to all 0s or all 1s
- Maximum power of jump required to reach the end of string
- Repeat substrings of the given String required number of times
- Minimum swaps required to convert one binary string to another

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.