word processor

We are building a word processor and we would like to implement a "reflow" functionality that also applies full justification to the text. Given an array containing lines of text and a new maximum width, re-flow the text to fit the new width. Each line should have the exact specified width. If any line is too short, insert '-' (as stand-ins for spaces) between words as equally as possible until it fits. Note: we are using '-' instead of spaces between words to make testing and visual verification of the results easier.

lines = [ "The day began as still as the",
          "night abruptly lighted with",
          "brilliant flame" ]

reflowAndJustify(lines, 24) ... "reflow lines and justify to length 24" =>

        [ "The--day--began-as-still",
          "as--the--night--abruptly",
          "lighted--with--brilliant",
          "flame" ] // <--- a single word on a line is not padded with spaces

Similar as LC 68

public class Main {

    public List<String> reflowAndJustify(String[] lines, int maxWidth) {
        
        List<String> words = new ArrayList<String>();
        for(int i = 0; i< lines.length; i++)
        {
            String[] word = lines[i].spit(" ");
            for(int j = 0; j< word.length; j++)
            {
                words.add(word[j]);
            }
        }

        List<String> result = new ArrayList<String>();
        //int remain = maxWidth;
        int currentTotalLen = 0;
        List<String> oneLine = new ArrayList<>();
        for(int i = 0; i< words.size(); i++)
        {
            ///单词总长 + 空格数(最少保证一个空格) + 当前单词 <= maxWidth
            if(oneLine.isEmpty() || currentTotalLen + oneLine.size() + words.get(i).length() <= maxWidth)
            {
                oneLine.add(words.get(i));
                currentTotalLen += words.get(i).length();
            }
            else
            {
                result.add(getOneLine(maxWidth, oneLine, currentTotalLen));
                oneLine = new ArrayList<>();
                oneLine.add(words.get(i));
                currentTotalLen = words.get(i).length();
            }
        }
        
        
        // 最后一行的处理稍微不一样一点
        //result.add(getOneLine(maxWidth, oneLine, currentTotalLen));
        StringBuilder sb = new StringBuilder();
        if(oneLine.size() == 1)
        {
            sb.append(oneLine.get(0));
        }
        else
        {
            for(int  i = 1; i< oneLine.size(); i++)
            {
                sb.append("-");
                sb.append(oneLine.get(i));
            }
            
            int extraSpace = maxWidth - sb.length();
            for(int j = 0; j< extraSpace; j++)
            {
                sb.append("-");
            }
        }
        result.add(sb.toString());
        
        return result;
    }
    
    public String getOneLine(int maxLen, List<String> line, int currentTotalLen)
    {
        StringBuilder sb = new StringBuilder();
        int extraSpace = maxLen-currentTotalLen;
        
        if(line.size() == 1)
        {
            String word = line.get(0);
            sb.append(word);
            for(int i = 0 ; i< extraSpace; i++)
            {
                sb.append("-");
            }
            return sb.toString();
        }
        
        int intervalCount = line.size()-1;
        int intervalPerSpace = extraSpace/intervalCount;
        int extraceSpaceFromfirstNIntervals = extraSpace%intervalCount;
        
        // 预处理space的长度
        String space = "";
        for(int i = 0; i< intervalPerSpace; i++)
        {
            space += "-";
        }
        
        sb.append(line.get(0));
        for(int j = 1; j< line.size(); j++)
        {
            sb.append(space);
            if(extraceSpaceFromfirstNIntervals != 0)
            {
                sb.append("-");
                extraceSpaceFromfirstNIntervals--;
            }
            sb.append(line.get(j));
        }

        return sb.toString();
    }
}

Last updated