Slower Java solution - How I approached the problem


  • 1
    C
    public class Solution {
        public List<String> fullJustify(String[] words, int maxWidth) {
            List<Line> paragraph = new ArrayList<Line>();
    
            Line current = new Line();
            int curlen = 0;
    
            for (String w : words) {
                if (curlen + w.length() > maxWidth) {
                    current.contents.get(current.contents.size() - 1).spaces = 0;
                    paragraph.add(current);
                    current = new Line();
                    curlen = 0;
                }
    
                current.contents.add(new Word(w, 1));
                curlen += w.length() + 1;
            }
    
            if (current.contents.size() > 0) {
                current.contents.get(current.contents.size() - 1).spaces = 0;
                paragraph.add(current);
            }
    
            List<String> result = new ArrayList<String>();
            for (int i = 0; i < paragraph.size() - 1; i++) {
                result.add(paragraph.get(i).justify(maxWidth));
            }
            result.add(paragraph.get(paragraph.size() - 1).padleftover(maxWidth));
    
            return result;
        }
    
        class Line {
            List<Word> contents;
    
            Line() {
                contents = new ArrayList<Word>();
            }
    
            int totalsize() {
                int size = 0;
                for (Word w : contents) {
                    size += w.size();
                }
                return size;
            }
    
            void pad(int maxlen) {
                int padding = maxlen - totalsize();
    
                if (contents.size() == 1) {
                    contents.get(0).spaces += padding;
                } else {
                    int idx = 0;
                    while (padding-- > 0) {
                        contents.get(idx++ % (contents.size() - 1)).spaces++;
                    }
                }
            }
    
            String padleftover(int maxlen) {
                StringBuilder result = new StringBuilder();
                for (Word w : contents) {
                    result.append(w.string());
                }
    
                char[] padding = new char[maxlen - result.length()];
                Arrays.fill(padding, ' ');
                result.append(new String(padding));
    
                return result.toString();
            }
    
            String justify(int maxlen) {
                pad(maxlen);
    
                StringBuilder result = new StringBuilder();
                for (Word w : contents) {
                    result.append(w.string());
                }
                return result.toString();
            }
        }
    
        class Word {
            char[] word;
            int spaces;
    
            Word(String word, int spaces) {
                this.word = word.toCharArray();
                this.spaces = spaces;
            }
    
            int size() {
                return word.length + spaces;
            }
    
            String string() {
                char[] result = new char[size()];
                Arrays.fill(result, ' ');
                for (int i = 0; i < word.length; i++) {
                    result[i] = word[i];
                }
                return new String(result);
            }
    
        }
    }
    

    Overly verbose, but hopefully my approach is clear from the code.


Log in to reply
 

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