Straightforward Java Simulation


  • 0
    O
    public class Solution {
        public List<String> fullJustify(String[] words, int maxWidth) {
            List<Line> lines = new ArrayList<>();
            Line curr = new Line();
            for (String word : words) {
                // If new word plus normal English space spills, create new line.
                if (curr.len != 0 && (curr.len + word.length() >= maxWidth)) {
                    lines.add(curr);
                    curr = new Line();
                }
                curr.add(word);
            }
            lines.add(curr);
            
            List<String> result = new ArrayList<>();
            StringBuilder sb;
            // Iterate everyline but the last.
            for (int a = 0; a < lines.size() - 1; a++) {
                sb = new StringBuilder();
                Line line = lines.get(a);
                if (line.words.size() <= 2) { // Special case but can also be generalized.
                    sb.append(line.words.get(0));
                    for (int b = 0; b < maxWidth - line.len; b++) sb.append(' ');
                    if (line.words.size() == 2) sb.append(' ').append(line.words.get(1));
                } else { // Print words on a line and pad accordingly.
                    // Minimum padding width.
                    int pad = (maxWidth - line.len) / (line.words.size() - 1);
                    // Number of spaces (from left) with extra padding.
                    int boost = (maxWidth - line.len) % (line.words.size() - 1);
                    int count = 0;
                    // Print every word but the last and pad accordingly.
                    for (int b = 0; b < line.words.size() - 1; b++) {
                        sb.append(line.words.get(b));
                        for (int c = 0; c <= pad; c++) sb.append(' ');
                        if (count++ < boost) sb.append(' ');
                    }
                    
                    // Print last word and don't pad.
                    sb.append(line.words.get(line.words.size() - 1));
                }
                
                result.add(sb.toString());
            }
            
            // Treat last line specially.
            sb = new StringBuilder();
            Line line = lines.get(lines.size() - 1);
            // Iterate every word but the last. Print each with a single English space.
            for (int a = 0; a < line.words.size() - 1; a++) {
                sb.append(line.words.get(a)).append(' ');
            }
            
            print last word but not followed by English space.
            sb.append(line.words.get(line.words.size() - 1));
    
            // Pad the last line with spaces.
            for (int a = 0; a < maxWidth - line.len; a++) sb.append(' ');
            
            result.add(sb.toString());
            return result;
        }
        
        // Data structure to represent a line of text.
        private static class Line {
            private List<String> words;
            private int len;
            private Line() {
                words = new ArrayList<>();
            }
            private int add(String word) {
                words.add(word);
                len += words.size() == 1 ? word.length() : word.length() + 1;
                return len;
            }
        }
    }

Log in to reply
 

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