Leetcode 824 Solution

This article provides solution to leetcode question 824 (number-of-lines-to-write-string)

https://leetcode.com/problems/number-of-lines-to-write-string

Solution

class Solution {
public:
    vector<int> numberOfLines(vector<int>& widths, string S) {
        int line_cnt = 1;
        int line_left = 100;

        for (auto ch: S)
        {
            auto width = widths[ch - 'a'];

            if (width > line_left)
            {
                line_cnt++;
                line_left = 100;
            }

            line_left -= width;
        }

        return {line_cnt, 100 - line_left};
    }
};