Count of lines required to write the given String
Last Updated :
13 Dec, 2022
Given a string str and an integer array width[] where:
width[0] = width of character ‘a’
width[1] = width of character ‘b’
…
width[25] = width of character ‘z’
The task is to find the number of lines it’ll take to write the string str on a paper and the width of the last line upto which it is occupied.
Note: The width of a line is 10 units.
Examples:
Input: str = “bbbcccdddaa”,
width[] = {4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
Output: (2, 8)
“bbbcccddd” will cover first line (9 * 1 = 9 units)
As ‘a’ has a width of 4 which cannot fit the remaining 1 unit in the first line.
It’ll have to be written in the second line.
So, next line will contain “aa” covering 4 * 2 = 8 units.
We need 1 full line and one line with width 8 units.
Input: str = “abcdefghijklmnopqrstuvwxyz”,
width[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
Output: (3, 6)
All the characters have the same width of 1. To write all 26 characters,
We need 2 full lines and one line with width 6 units.
Approach: We will write each character in the string str one by one. As we write a character, we immediately update (lines, width) that keeps track of how many lines we have used till now and what is the length of the used space in the last line.
If the width[char] in str fits our current line, we will add it. Otherwise, we will start with a new line
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
pair< int , int > numberOfLines(string S, int *widths)
{
if (S.empty())
return {0, 0};
int lines = 1, width = 0;
for ( auto character : S)
{
int w = widths[character - 'a' ];
width += w;
if (width >= 10)
{
lines++;
width = w;
}
}
return {lines, width};
}
int main()
{
string S = "bbbcccdddaa" ;
int widths[] = {4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
pair< int , int > ans = numberOfLines(S, widths);
cout << ans.first << " " << ans.second << endl;
return 0;
}
|
Java
class GFG
{
static int [] numberOfLines(String S, int []widths)
{
if (S.isEmpty())
return new int []{ 0 , 0 };
int lines = 1 , width = 0 ;
for ( char character : S.toCharArray())
{
int w = widths[character - 'a' ];
width += w;
if (width >= 10 )
{
lines++;
width = w;
}
}
return new int []{lines, width};
}
public static void main(String[] args)
{
String S = "bbbcccdddaa" ;
int widths[] = { 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
int []ans = numberOfLines(S, widths);
System.out.print(ans[ 0 ]+ " " + ans[ 1 ] + "\n" );
}
}
|
Python3
def numberOfLines(S, widths):
if (S = = ""):
return 0 , 0
lines, width = 1 , 0
for c in S:
w = widths[ ord (c) - ord ( 'a' )]
width + = w
if width > 10 :
lines + = 1
width = w
return lines, width
S = "bbbcccdddaa"
Widths = [ 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ]
print (numberOfLines(S, Widths))
|
C#
using System;
class GFG
{
static int [] numberOfLines(String S, int []widths)
{
if (S.Length == 0)
return new int []{0, 0};
int lines = 1, width = 0;
foreach ( char character in S.ToCharArray())
{
int w = widths[character - 'a' ];
width += w;
if (width >= 10)
{
lines++;
width = w;
}
}
return new int []{lines, width};
}
public static void Main(String[] args)
{
String S = "bbbcccdddaa" ;
int []widths = {4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int []ans = numberOfLines(S, widths);
Console.Write(ans[0]+ " " + ans[1] + "\n" );
}
}
|
Javascript
<script>
function numberOfLines(S,widths)
{
if (S.length==0)
return [0, 0];
let lines = 1, width = 0;
for (let character of S.split( "" ))
{
let w = widths[character.charCodeAt(0) - 'a' .charCodeAt(0)];
width += w;
if (width >= 10)
{
lines++;
width = w;
}
}
return [lines, width];
}
let S = "bbbcccdddaa" ;
let widths = [4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
let ans = numberOfLines(S, widths);
document.write(ans[0]+ " " + ans[1] + "<br>" );
</script>
|
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(2) ? O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...