9ms concise c++ solution


  • 0
    C

    Thanks @kylejao for his brilliant idea, here is the c++ version:

    The idea is to count the valid positions that will be used to fill the target string, and the final result is simply count/str.size();

    int wordsTyping(vector<string>& strs, int rows, int cols) {
        string str = "";
        for (string& s: strs)
            str += s + ' ';
        int count = 0, n = str.size();
        while (rows--) {
            count += cols;
            if (str[count % n] == ' ') count++;
            else {
                while (count % n && str[count % n - 1] != ' ') 
                    count--; 
            }
        }
        return count / n;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.