68. Text Justification

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.

You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' ' when necessary so that each line has exactly L characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

For the last line of text, it should be left justified and no extra space is inserted between words.

For example,

words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Note: Each word is guaranteed not to exceed L in length.

  • if there is only one word in the line , it should be left justified.
  • if it is the last line, it should be left justified too.
  • if the spaces cannot be evenly distributed between words, left words[Not just one words] shoule contains more space than the right ones. i.e. the extras space should be event distributed from the left.
public class Solution {
    List<String > res = new ArrayList<>();
    public List<String> fullJustify(String[] words, int maxWidth) {
        int count =words[0].length();
        int start = 0;
        for(int i=1; i<= words.length; i++){
            if( i == words.length){

                justify(words, start, i, maxWidth);
            }else if( count + words[i].length() + 1<= maxWidth ){
                count += words[i].length() +1;
                continue;
            }else{
                justify(words, start, i, maxWidth);// left inclusive, right exclusive;
                start = i;
                count = words[i].length();
            }
        }

        return res;
    }

    void justify(String[] words, int start, int end, int L){

        char[] array = new char[L];
        int k = 0;

        if(end == start +1){
            String w = words[start];
            while(k< L){
                array[k] = k< w.length() ? w.charAt(k) : ' ';
                k++;
            } 
            res.add(new String(array));
            return; 
        }

        if(end == words.length){
            for(int i =start; i< end; i++){
                String w = words[i];
                int j =0;
                while(j < w.length()){
                    array[k++] = w.charAt(j++);
                }
                array[k++] = ' ';
            }
            while(k < L) array[k++] = ' ';
            res.add(new String(array));
            return;
        }

        int count = 0;

        for(int i=start; i<end; i++){
            count += words[i].length();
        }
        int spaces = (L - count)/(end -start -1);
        int extras = (L - count)%(end -start -1);

        for(int i= start; i< end; i++){
            String w = words[i];
            int j = 0;
            while(j < w.length()){
                array[k++] = w.charAt(j++);
            }

            if(i == end -1) break;

            j = 0;
            while(j++ < spaces){
                array[k++] = ' ';
            }
            if(extras-- > 0) {
                array[k++] = ' ';
            }
        }

        res.add(new String(array));

    }
}
public class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        int len = 0;
        List<String> res = new ArrayList<>();
        List<String> list = new ArrayList<>();

        for(int i=0; i<words.length; i++){
            if(len + words[i].length() + list.size() <= maxWidth){
                list.add(words[i]);
                len += words[i].length();
            }else{
                justify(list, res, len, maxWidth);
                list.clear();
                list.add(words[i]);
                len = words[i].length();
            }
        }

        if(!list.isEmpty()){
            leftJustify(list, res, len, maxWidth);
        }

        return res;
    }
    void leftJustify(List<String> list, List<String> res, int len, int maxWidth){
        char[] chars = new char[maxWidth];
        char[] first = list.get(0).toCharArray();
        System.arraycopy(first, 0, chars, 0, first.length);
        if( list.size() == 1){
            Arrays.fill(chars, first.length, chars.length, ' ');
            res.add(new String(chars));
            return;
        }else{
            int dis = 1;
            int stop = first.length;
            for(int i=1; i< list.size(); i++){
                Arrays.fill(chars, stop, stop+1, ' ');
                stop++;
                char[] word = list.get(i).toCharArray();
                System.arraycopy(word, 0, chars, stop, word.length);
                stop += word.length;
            }
            if(stop < chars.length){
                Arrays.fill(chars, stop, chars.length, ' ');
            }
            res.add(new String(chars));
        }
    }

    void justify(List<String> list, List<String> res, int len, int maxWidth){
        char[] chars = new char[maxWidth];

        char[] first = list.get(0).toCharArray();
        System.arraycopy(first, 0, chars, 0, first.length);


        if (list.size() == 1) {
            Arrays.fill(chars, first.length, chars.length, ' ');
            res.add(new String(chars));
            return;
        } else {
            int dis = (maxWidth - len) / (list.size() - 1);
            int extra = list.size() > 2 ? (maxWidth - len) % (list.size() - 1) : 0;
            int stop = first.length;

            for (int i = 1; i < list.size(); i++) {
                Arrays.fill(chars, stop, stop + dis, ' ');
                stop += dis;
                if(extra > 0 ){
                    Arrays.fill(chars, stop, stop + 1, ' ');
                    stop += 1;
                    extra -= 1;
                }
                char[] word = list.get(i).toCharArray();
                System.arraycopy(word, 0, chars, stop, word.length);
                stop += word.length;

            }
            res.add(new String(chars));
        }
        return;
    }
}

results matching ""

    No results matching ""