--- tags: leetcode --- # [418. Sentence Screen Fitting](https://leetcode.com/problems/sentence-screen-fitting/) --- # My Solution ## The Key Idea for Solving This Coding Question ## C++ Code ```cpp= class Solution { public: int wordsTyping(vector<string> &sentence, int rows, int cols) { string oneLine; for (auto &word : sentence) { oneLine = oneLine + word + " "; } int cursor = 0, lineLen = oneLine.size(); for (int r = 0; r < rows; ++r) { cursor += cols; if (oneLine[cursor % lineLen] == ' ') { ++cursor; } else { // oneLine[cursor] != ' ' is true. while(cursor > 0 && oneLine[(cursor - 1) % lineLen] != ' ') { --cursor; } } } return cursor / lineLen; } }; ``` ## Time Complexity ## Space Complexity # Miscellaneous <!-- # Test Cases ``` ["hello","world"] 2 8 ``` ``` ["a", "bcd", "e"] 3 6 ``` ``` ["i","had","apple","pie"] 4 5 ``` ``` ["f","p","a"] 8 7 ``` -->
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up