Compute Number of Lines To Write String (Wordwrap)


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 :
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 :
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].

String Word-wrap Algorithm

Let’s use a variable to store the current line number, and the current column number. As we go through the characters in the string, we add up the width, if the column exceeds the max width which is 100, we need to rewind and jump to next row (increment the row number), meanwhile, we can’t put the last character, so the column number will be equal to the width of the last letter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
    vector<int> numberOfLines(vector<int>& widths, string S) {
        int row = 0;
        int r = 1;
        for (int i = 0; i < S.size(); ++ i) {            
            int w = widths[S[i] - 97];
            row += w;
            if (row > 100) {
                r ++;
                row = w;
            }
        }
        return {r, row};        
    }
};
class Solution {
public:
    vector<int> numberOfLines(vector<int>& widths, string S) {
        int row = 0;
        int r = 1;
        for (int i = 0; i < S.size(); ++ i) {            
            int w = widths[S[i] - 97];
            row += w;
            if (row > 100) {
                r ++;
                row = w;
            }
        }
        return {r, row};        
    }
};

The above C++ implementation takes O(1) space complexity and O(N) time where N is the length of the String.

Converting to Java – need the charAt to get the char at the specified index.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
    public int[] numberOfLines(int[] widths, String S) {
        int row = 0;
        int r = 1;
        for (int i = 0; i < S.length(); ++ i) {            
            int w = widths[S.charAt(i) - 97];
            row += w;
            if (row > 100) {
                r ++;
                row = w;
            }
        }
        return new int[] {r, row};
    }
}
class Solution {
    public int[] numberOfLines(int[] widths, String S) {
        int row = 0;
        int r = 1;
        for (int i = 0; i < S.length(); ++ i) {            
            int w = widths[S.charAt(i) - 97];
            row += w;
            if (row > 100) {
                r ++;
                row = w;
            }
        }
        return new int[] {r, row};
    }
}

Also, in Java, we can iterate the characters by converting the source string to char array using toCharArray() method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
    public int[] numberOfLines(int[] widths, String S) {
        int row = 0;
        int r = 1;
        for (char c: S.toCharArray()) {            
            int w = widths[c - 97];
            row += w;
            if (row > 100) {
                r ++;
                row = w;
            }
        }
        return new int[] {r, row};
    }
}
class Solution {
    public int[] numberOfLines(int[] widths, String S) {
        int row = 0;
        int r = 1;
        for (char c: S.toCharArray()) {            
            int w = widths[c - 97];
            row += w;
            if (row > 100) {
                r ++;
                row = w;
            }
        }
        return new int[] {r, row};
    }
}

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
578 words
Last Post: How to Reverse Words in a String?
Next Post: How to Compute the Number Complement for Integers?

The Permanent URL is: Compute Number of Lines To Write String (Wordwrap)

Leave a Reply