Categories:

Tags:



Problem

You are given a string s of lowercase English letters and an array widths denoting how many pixels wide each lowercase English letter is. Specifically, widths[0] is the width of 'a', widths[1] is the width of 'b', and so on.

You are trying to write s across several lines, where each line is no longer than 100 pixels. Starting at the beginning of s, write as many letters on the first line such that the total width does not exceed 100 pixels. Then, from where you stopped in s, continue writing as many letters as you can on the second line. Continue this process until you have written all of s.

Return an array result of length 2 where:

  • result[0] is the total number of lines.
  • result[1] is the width of the last line in pixels.

Constraints

  • widths.length == 26
  • 2 <= widths[i] <= 10
  • 1 <= s.length <= 1000
  • s contains only lowercase English letters.

Solution

The problem Number of Lines To Write String can be solved by simply counting the number of pixels for each rows.

Implementation

static const int fast_io = []()
{
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    return 0;
}();

class Solution
{
  public:
    vector<int> numberOfLines(vector<int> &widths, string s)
    {
        vector<int> ret = {1, 0};
        for (char &ch : s)
        {
            if (ret[1] + widths[ch - 'a'] > 100)
            {
                ret[0] += 1;
                ret[1] = 0;
            }
            ret[1] += widths[ch - 'a'];
        }

        return ret;
    }
};