Leetcode 806 Number of Lines to Write string string lines

Source: Internet
Author: User

Title Description:

We is to write the letters of a given string S, from left to right into lines. Each line have maximum width units, and if writing a letter would cause the width of the "line" exceed units, it I s written on the next line. We are given an array widths, an array where widths[0] is the width of ' a ', widths[1] is the width of ' B ', ..., and widths [+] is the width of ' z '.

Now answer, Questions:how many lines has at least one character from S, and what's the width used by the last such L Ine? Return your answer as an integer list of length 2.

example:input:widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10] S = " ABCDEFGHIJKLMNOPQRSTUVWXYZ "Output: [3] explanation:all letters has the same length of 10.
To write all letters, we need the lines and one line with the units. 
Example:
Input: 
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "BBBCCCDDDAAA"
Output: [2, 4]
explanation: All 
letters except ' a ' has the same length of ten, and 
"Bbbcccdddaa" would cover 9 * Ten + 2 * 4 = 98 units.
For the last ' a ', it's written on the second line because there are only 2 units left in the first line
.
The answer is 2 lines, plus 4 units on the second line.

Note:the length of S'll is in the range [1, 1000]. S would only contain lowercase letters. Widths is an array of length 26. Widths[i] would be in the range of [2, 10].

Translation:

1. Give a length of 26 array widths, save 26 letters to print out the screen width, and each line of screen width is only 100, more than this width, the letter can only print to the next line, give a string, find out the last letter after printing the number of rows, columns.

Ideas:

1. Print string, record number of rows lines, number of columns POS, if POS exceeds 100, print to next line

2. Time complexity O (n)

Code:

Class Solution {public
:
    vector<int> numberoflines (vector<int>& widths, string S) 
    {
        int n=s.size (), lines=1,pos=0;
        for (int i=0;i<n;i++)
        {
            pos + = widths[s[i]-' a '];
            if (pos>100)//write out
            {
                lines++;p os=0;//down a line to write
                i--;//just don't write the word
            }
        }
        return {Lines,pos} ;
    }
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.