Number of Lines To Write String in Python

When writing a string onto lines with a maximum width of 100 units, we need to determine how many lines are required and the width used by the last line. Each character has a specific width given in a widths array where widths[0] represents the width of 'a', widths[1] represents 'b', and so on.

The problem asks us to find ?

  • How many lines have at least one character from the string
  • What is the width used by the last line

Algorithm

We follow these steps ?

  • Initialize lines = 1 and current_width = 0
  • For each character in the string:
    • Add the character's width to current_width
    • If current_width > 100, start a new line and reset the width
  • Return [lines, current_width]

Example

Let's trace through the example with widths array and string "bbbcccdddaaa" ?

def numberOfLines(widths, S):
    lines = 1
    current_width = 0
    
    for char in S:
        char_width = widths[ord(char) - ord('a')]
        
        # If adding this character exceeds 100, start new line
        if current_width + char_width > 100:
            lines += 1
            current_width = char_width
        else:
            current_width += char_width
    
    return [lines, current_width]

# Test with the example
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"

result = numberOfLines(widths, S)
print(f"Lines needed: {result[0]}, Last line width: {result[1]}")
Lines needed: 2, Last line width: 4

Step-by-Step Trace

For the string "bbbcccdddaaa" with the given widths ?

def numberOfLines_detailed(widths, S):
    lines = 1
    current_width = 0
    
    print(f"Processing string: '{S}'")
    print(f"Width of 'a': {widths[0]}, Width of 'b': {widths[1]}, Width of 'c': {widths[2]}, Width of 'd': {widths[3]}")
    print()
    
    for i, char in enumerate(S):
        char_width = widths[ord(char) - ord('a')]
        
        if current_width + char_width > 100:
            lines += 1
            current_width = char_width
            print(f"Character '{char}' (width {char_width}) starts new line {lines}")
        else:
            current_width += char_width
            print(f"Character '{char}' (width {char_width}) added to line {lines}, total width: {current_width}")
    
    print(f"\nFinal result: {lines} lines, last line width: {current_width}")
    return [lines, current_width]

# Detailed trace
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"

result = numberOfLines_detailed(widths, S)
Processing string: 'bbbcccdddaaa'
Width of 'a': 4, Width of 'b': 10, Width of 'c': 10, Width of 'd': 10

Character 'b' (width 10) added to line 1, total width: 10
Character 'b' (width 10) added to line 1, total width: 20
Character 'b' (width 10) added to line 1, total width: 30
Character 'c' (width 10) added to line 1, total width: 40
Character 'c' (width 10) added to line 1, total width: 50
Character 'c' (width 10) added to line 1, total width: 60
Character 'd' (width 10) added to line 1, total width: 70
Character 'd' (width 10) added to line 1, total width: 80
Character 'd' (width 10) added to line 1, total width: 90
Character 'a' (width 4) added to line 1, total width: 94
Character 'a' (width 4) added to line 1, total width: 98
Character 'a' (width 4) starts new line 2

Final result: 2 lines, last line width: 4

Key Points

  • We convert characters to array indices using ord(char) - ord('a')
  • When adding a character would exceed 100 units, we start a new line
  • The last character 'a' cannot fit on line 1 (98 + 4 > 100), so it goes to line 2

Conclusion

This algorithm efficiently tracks line usage by checking if each character fits on the current line. The time complexity is O(n) where n is the string length, and space complexity is O(1).

Updated on: 2026-03-25T08:50:54+05:30

545 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements