806. Number of Lines To Write String LeetCode Solution

In this guide, you will get 806. Number of Lines To Write String LeetCode Solution with the best time and space complexity. The solution to Number of Lines To Write String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Lines To Write String solution in C++
  4. Number of Lines To Write String solution in Java
  5. Number of Lines To Write String solution in Python
  6. Additional Resources
806. Number of Lines To Write String LeetCode Solution image

Problem Statement of Number of Lines To Write String

You are given a string s of lowercase English letters and an array widths denoting how many pixels wide each lowercase English letter is. Specifically, widths[0] is the width of ‘a’, widths[1] is the width of ‘b’, and so on.
You are trying to write s across several lines, where each line is no longer than 100 pixels. Starting at the beginning of s, write as many letters on the first line such that the total width does not exceed 100 pixels. Then, from where you stopped in s, continue writing as many letters as you can on the second line. Continue this process until you have written all of s.
Return an array result of length 2 where:

result[0] is the total number of lines.
result[1] is the width of the last line in pixels.

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: You can write s as follows:
abcdefghij // 100 pixels wide
klmnopqrst // 100 pixels wide
uvwxyz // 60 pixels wide
There are a total of 3 lines, and the last line is 60 pixels wide.
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: You can write s as follows:
bbbcccdddaa // 98 pixels wide
a // 4 pixels wide
There are a total of 2 lines, and the last line is 4 pixels wide.

Constraints:

widths.length == 26
2 <= widths[i] <= 10
1 <= s.length <= 1000
s contains only lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

806. Number of Lines To Write String LeetCode Solution in C++

class Solution {
 public:
  vector<int> numberOfLines(vector<int>& widths, string s) {
    int numLines = 1;
    int runningWidth = 0;

    for (const char c : s) {
      const int width = widths[c - 'a'];
      if (runningWidth + width <= 100) {
        runningWidth += width;
      } else {
        ++numLines;
        runningWidth = width;
      }
    }

    return {numLines, runningWidth};
  }
};
/* code provided by PROGIEZ */

806. Number of Lines To Write String LeetCode Solution in Java

class Solution {
  public int[] numberOfLines(int[] widths, String s) {
    int numLines = 1;
    int runningWidth = 0;

    for (final char c : s.toCharArray()) {
      final int width = widths[c - 'a'];
      if (runningWidth + width <= 100) {
        runningWidth += width;
      } else {
        ++numLines;
        runningWidth = width;
      }
    }

    return new int[] {numLines, runningWidth};
  }
}
// code provided by PROGIEZ

806. Number of Lines To Write String LeetCode Solution in Python

class Solution:
  def numberOfLines(self, widths: list[int], s: str) -> list[int]:
    numLines = 1
    runningWidth = 0

    for c in s:
      width = widths[string.ascii_lowercase.index(c)]
      if runningWidth + width <= 100:
        runningWidth += width
      else:
        numLines += 1
        runningWidth = width

    return [numLines, runningWidth]
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.