Number of Lines To Write String

Question

We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an array widths, an array where widths[0] is the width of ‘a’, widths[1] is the width of ‘b’, …, and widths[25] is the width of ‘z’.

Now answer two questions: how many lines have at least one character from S, and what is the width used by the last such line? Return your answer as an integer list of length 2.

Example 1:

Input:
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "abcdefghijklmnopqrstuvwxyz"
Output: [3, 60]
Explanation:
All letters have the same length of 10. To write all 26 letters,
we need two full lines and one line with 60 units.

Example 2:

Input:
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "bbbcccdddaaa"
Output: [2, 4]
Explanation:
All letters except 'a' have the same length of 10, and
"bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 units.
For the last 'a', it is written on the second line because
there is only 2 units left in the first line.
So the answer is 2 lines, plus 4 units in the second line.

Note:

The length of S will be in the range [1, 1000]. S will only contain lowercase letters. widths is an array of length 26. widths[i] will be in the range of [2, 10].

Solution:

class Solution {
    public int[] numberOfLines(int[] widths, String S) {
         // initialize the number of lines to 1
        // as there will be at least one character as input.
        int numOfLines = 1;
        // initialize the total line width to 0.
        int lineWidth = 0;

        //iterate through each character of String S                              
        for (char c: S.toCharArray())
          {

            /* get the width of iterated
            character from widths array. the widths array index is calculated
            using the difference between ascii value of the iterated character
            and character 'a' which will be 0 (97-97) if the iterated character
            is 'a', 1(98-97) if the iterated character is b and so on.. */

            int charWidth = widths[c - 'a'];
            // increment the total line width with each iterated character width
            lineWidth += charWidth;
            // if the total line width exceeds 100 then increment the number of
            //lines by 1 and re initialize total line width to the width of last
            //iterated character as it is the last iterated character's width
            //that made the total line width exceed 100.

            if (lineWidth > 100)  
            {
                numOfLines++;
                lineWidth = charWidth;
            }
        }

        // return the total number of lines and the last line width
        return new int[]{numOfLines, lineWidth};
    }
}

Test Cases

  1. An input String with only one character and has some width(in range 1-10).For eg. S= “a”, widths = [5].
  2. An input String with unique 10 character each of whose width is 10 (total width = 100). For eg. S= “abcqwhjsit”,widths= [10,10,10,1,1,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
  3. An input String with some repetitive characters(total 10) each of whose width is 10(total width = 100). For eg. S= “aaqbbaaabt”, widths = [10,10,1,1,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
  4. An input String with unique characters such that their total width is less than 100.For eg. S= “abcqwhjs”, widths = [1,2,3,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
  5. An input String with some repetitive character such that their total width is less than 100. For eg. S= “aabc”, widths = [0,2,3,1,0,0,0,0,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
  6. An input String with unique character such that their total width is more than 100. For eg. S= “abcqwhjsdzyx”, widths= [10,10,10,10,1,1,1,10,1,10,0,1,1,0,1,10,10,10,10,1,10,10,10,10,10,10]
  7. An input String with some repetitive character such that their total width is more than 100. For eg. S= “abcqwcjsdbaa”, widths = [10,10,10,10,0,0,0,0,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
  8. An input String with maximum character allowed(1000) and all of them use maximum width that is 10.
  9. An empty String with some width.For eg. S= “”, widths=
    [1,1,1,1,0,0,0,0,10,10,10,10,10,10,10,10,10,10,10,10,10,4,5,8,9,10]
  10. An input String with uppercase characters with some width(in range 1-10).This negative testing condition is not handled here, the output will depend on the requirement.
  11. An input String with special characters with some out of range width(>10). This negative testing condition is not handled here, the output will depend on the requirement.
  12. A null String with some width. This will throw an exception (negative testing, condition not handled here).

See also