Most difficult OA || FAANG- 2023
Anonymous User
210

Given a string containing a sequence of words separated by whitespace-like characters, add white spaces so that the words can be printed neatly given a character limit per line. Assume that there are no words that exceed the line’s width.

Your solution will take as input:

A string of words separated by whitespace-like characters
An integer representing the length which each line in the output must be
If there is any amount or type of whitespace between two words in the input string, they are considered separate words with the exception of hyphenated words (more information below about how to treat hyphenated words). For example, “hello world” and “hello world” lead to the same output.

Consider the following sample input:

Copy code

Copy code
"a cat is an animal" 6

The function should output the following:

Copy code

Copy code
['a cat', 'is an', 'animal']

Each element in the output is a line, each line has 6 characters (including whitespace characters).

Even Whitespace Distribution
There’s a catch: whitespace should be distributed evenly between words in a line. When not possible, the extra whitespace should be appended to the end of the line.

Therefore, given the following input:

Copy code

Copy code
"cat is an animal and so is a dog" 12

Your function should return:

Copy code

Copy code
["cat is an ", "animal and", "so is a dog "]

In this example, "cat is an " has two spaces between each word because the words themselves have 7 characters, which leaves 5 for white spaces to be added. This allows us to add two spaces between each word, with one white space left over to be added to the end because it couldn’t be evenly added between the words.

Line two, "animal and", can only contain two words, so we are allowed to add all the whitespaces between the two words.

Single Word Lines
If any of the lines end up having only 1 word on them, then instead of appending the extra whitespace to the end of the line, we want to center the word. If the word can’t be exactly centered, as before, the extra white space should be added to the end of the line.

For example , the input:

Copy code

Copy code
"human" 8

Will output

Copy code

Copy code
[" human "]

There are 3 extra white space characters so the extra whitespace goes to the end.

Hyphenated Words
Hyphenated words (”self-inflicted”, “pre-approval”, “mother-in-law”) should be kept on one line if possible, but if the first part of the hyphenated word including the hyphen (”self-” from “self-inflicted”) fits while the whole word doesn’t, then the word should be split up.

Hyphenated words can have any amount of whitespace around the hyphens, but they should be treated as if there is no white space around the hyphens. For example, an input of “self-inflicted” and “self - inflicted” should lead to the same result.

For example, given the following input:

Copy code

Copy code
"auto-complete is my go - to" 8

Your function should return:

Copy code

Copy code
[" auto- ", "complete", "is my", " go-to "]

In this example, “auto-complete” cannot fit on the line, so we split it up and have “auto-” on the first line. Even with the extra whitespace, we treat it as “go-to” , which fits on one line so there is no need to split it up.

Find the saem leetcode question on google or nearest same?

I coded this one buts its does pass all the test case any suggestion on the same:

def format_text(text, max_width):
  
  words = text.split()
  lines = []
  current_line = []
  current_len = 0
  
  for word in words:
    if '-' in word:
      # Split on hyphen
      hyphen_idx = word.index('-')
      first, second = word[:hyphen_idx+1], word[hyphen_idx+1:]
      
      if current_len + len(first) <= max_width:
        current_line.append(first)
        current_len += len(first) + 1
      else:
        finalize_line(lines, current_line, max_width)
        current_line = [first]
        current_len = len(first) + 1
        
      if current_len + len(second) <= max_width:
        current_line.append(second)
        current_len += len(second) + 1
      else:
        finalize_line(lines, current_line, max_width)
        current_line = [second]  
        current_len = len(second) + 1
            
    else:
      if current_len + len(word) <= max_width:
        current_line.append(word)
        current_len += len(word) + 1
      else:
        finalize_line(lines, current_line, max_width)
        current_line = [word]
        current_len = len(word) + 1
        
  if current_line:
    finalize_line(lines, current_line, max_width)  
    
  return lines

def finalize_line(lines, current_line, max_width):
  num_spaces = max_width - sum(len(w) for w in current_line)
  if len(current_line) == 1:
    centered_space = num_spaces // 2
    current_line[0] += ' ' * centered_space
    current_line[0] += ' ' * (num_spaces - centered_space) 
  else:
    space, extra = divmod(num_spaces, len(current_line) - 1)
    for i in range(extra):
      current_line[i] += ' '
    current_line = [' ' * space + w for w in current_line]
  
  line = ''.join(current_line)
  lines.append(line)
  current_line = []
Comments (0)