C# solution with comments


  • 0
    public IList<string> FullJustify(string[] words, int maxWidth) {
            List<string> sentences = new List<string>();
            int totalWords = words.Length;
            int start = 0;
            while(start < totalWords)
            {
                int i = start;
                List<string> curRow = new List<string>();
                int rowLength = 0;
                int wordCount = 0;
                // for every n words there are n-1 single spaces.
                // until adding next word will not exceed max length
                while(i < totalWords && rowLength + wordCount + words[i].Length <= maxWidth)
                {
                    curRow.Add(words[i]);
                    rowLength += words[i].Length;
                    wordCount++;
                    i++;
                }
                
                if (i == start)
                {
                    // The word was larger than maxlength;
                    sentences.Add(words[start]);
                    start++;
                    continue;
                }
                
                if (rowLength + wordCount -1 == maxWidth)
                {
                    sentences.Add(string.Join(" ", curRow));
                }
                else {
                    int spacesToAdd = (maxWidth - (rowLength + wordCount -1));
                    
                    // Last sentence needs to be l-justified or there is only one word.
                    if (wordCount == 1 || i == totalWords)
                    {
                        sentences.Add(string.Join(" ", curRow) + GenSpace(spacesToAdd));
                    }
                    else {
                        // Check if we can distribute spacesToAdd among wordCount -1 words
                        int unevenSpaces = spacesToAdd % (wordCount -1);
                        int perWordSpaces = spacesToAdd / (wordCount -1);
                        
                        // Add one to perWordSpaces to account for the single space we added to rowlength.
                        if (unevenSpaces == 0)
                        {
                            sentences.Add(string.Join(GenSpace(perWordSpaces + 1), curRow));
                        }
                        else {
                            //Redistibute the uneven spaces to the left of the word builder.
                            string row = "";
                            for(int p = 0; p < wordCount-1; p++)
                            {
                                row += curRow.ElementAt(p) + GenSpace(unevenSpaces > 0 ? perWordSpaces + 2 : perWordSpaces + 1);
                                unevenSpaces--;
                            }
                            
                            row += curRow.Last();
                            sentences.Add(row);
                        }
                    }
                }
                
                start = i;
            }
            
            return sentences;
        }
        
        private string GenSpace(int len)
        {
            string spaces = "";
            for(int i = 0; i < len; i++)
            {
                spaces = spaces + " ";
            }
            
            return spaces;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.