Leetcode 418 Solution

This article provides solution to leetcode question 418 (sentence-screen-fitting)

https://leetcode.com/problems/sentence-screen-fitting

Solution

class Solution {
public:
    int wordsTyping(vector<string>& sentence, int rows, int cols) {
        if (sentence.empty())
            return 0;

        string full;

        for (auto word : sentence)
            full += word + " ";

        int start = 0;
        for (int i = 0; i < rows; i++)
        {
            start += cols;
            if (full[start % full.size()] == ' ')
                start++;
            while (start > 0 && full[(start - 1) % full.size()] != ' ')
                start--;
        }

        return start / full.size();
    }
};