
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Sentence Screen Fitting in C++
Suppose we have a rows x cols screen and a sentence represented by a list of non-empty words, so we have to find how many times the given sentence can be fitted on the screen. There are certain properties −
A word will not be split into two lines.
The order of words in the sentence must not be changed.
There will be only one space between two words.
The total number of words in the sentence won't exceed 100.
The length of each word is greater than 0 but less than 10.
1 ≤ rows, cols ≤ 20,000.
So if the input is like rows = 3 and cols = 6, and the sentence is [“a”, “bcd”, “e”], then the output will be 2.
To solve this, we will follow these steps −
Define a map dp, set ret := 0, n := size of sentence array
while row is not 0
start := ret mod n, len := -l and cnt := 0
if start is not present in dp, then
while 1 + len + size of sentence[(start + cnt) mod n] <= cols
len := 1 + len + sentence[(start + cnt) mod n]
increase cnt by 1
dp[start] := cnt
ret := ret + cnt
otherwise ret := ret + dp[start]
row := row – 1
return ret/n
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: int wordsTyping(vector<string>& sentence, int rows, int cols) { unordered_map <int, int> dp; int ret = 0; int n = sentence.size(); while(rows--){ int start = ret % n; int len = -1; int cnt = 0; if(!dp.count(start)){ while(1 + len + (int)sentence[(start + cnt) % n].size() <= cols){ len = 1 + len + sentence[(start + cnt) % n].size(); cnt++; } dp[start] = cnt; ret += cnt; } else{ ret += dp[start]; } } return ret / n; } }; main(){ vector<string> v = {"a","bcd","e"}; Solution ob; cout << (ob.wordsTyping(v, 3, 6)); }
Input
["a","bcd","e"] 3 6
Output
2
- Related Articles
- Fitting Shelves Problem in C++
- Sentence Similarity II in C++
- Curve Fitting Models in Software Engineering
- Regression Analysis and the Best Fitting Line using C++
- Rearrange Words in a Sentence in C++
- Finding the smallest fitting number in JavaScript
- Split the sentence into words in C++
- Count palindrome words in a sentence in C++
- Print longest palindrome word in a sentence in C Program
- Count spaces, uppercase and lowercase in a sentence using C
- C# Program to convert first character uppercase in a sentence
- C Program to convert first character uppercase in a sentence
- C program to count a letter repeated in a sentence.
- C++ program to Reverse a Sentence Using Recursion
- C# Program to replace a character with asterisks in a sentence
