Share my Java code. There are so many corner cases.


  • 0
    M
    public class Solution {
        public List<String> fullJustify(String[] words, int L) {
            List<String> result = new LinkedList<String>();
            int index = 0;
            while(index < words.length){
                int start = index;
                int holdLen = words[start].length();
                int wordLen = words[start].length();
                int lastIndex = index;
                index++;
                if(holdLen < L){
                    while(index < words.length){
                        if(holdLen + 1 + words[index].length() > L){
                            break;
                        }
                        holdLen += 1 + words[index].length();
                        wordLen += words[index].length();
                        
                        index++;
                    }
                    lastIndex = index - 1;
                }
                // System.out.println("from index = " + start + " to lastindex = " + lastIndex + " wordlen :" + wordLen);
                if(lastIndex == words.length - 1){
                    if(start == lastIndex){
                        StringBuilder res = new StringBuilder("");
                        res.append(words[start]);
                        for(int i = 0; i < L - words[start].length(); i++){
                            res.append(" ");
                        }
                        result.add(res.toString());
                    }else{
                        StringBuilder res = new StringBuilder("");
                        for(int i = start; i <= lastIndex; i++){
                            res.append(words[i]);
                            if(i != lastIndex){
                                res.append(" ");
                            }
                        }
                        boolean containsPeriod = words[lastIndex].charAt(words[lastIndex].length() - 1) == '.';
                        if(!containsPeriod){
                            res.append(".");
                        }
                        int tmp = res.length();
                        for(int i = 0; i < L - tmp; i++){
                            res.append(" ");
                        }                    
                        result.add(res.toString());
                    }
                }else if(start == lastIndex){
                        StringBuilder res = new StringBuilder("");
                        res.append(words[start]);
                        for(int i = 0; i < L - words[start].length(); i++){
                            res.append(" ");
                        }
                        result.add(res.toString());
                }else{
                    // cur Line : index -> lastIndex
                    int gapNum = lastIndex - start;
                    int spaceLen = (L - wordLen) / gapNum;
                    System.out.println("spaceLen : " + spaceLen);
                    int extraLen = (L - wordLen) % gapNum;
                    System.out.println("extraLen : " + extraLen);
                    StringBuilder res = new StringBuilder("");
                    
                    for(int i = start; i <= lastIndex; i++){
                        res.append(words[i]);
                        if(i != lastIndex){
                            if(extraLen != 0){
                                // index, index + 1, ... index + extraLen - 1 : space number == spaceLen + 1
                                // ohterwise : space number == spaceLen
                                if(i <= start + extraLen - 1){
                                    for(int j = 0; j < spaceLen + 1; j++){
                                        res.append(" ");
                                    }
                                }else{
                                    for(int j = 0; j < spaceLen; j++){
                                        res.append(" ");
                                    }
                                }
                            }else{
                                for(int j = 0; j < spaceLen; j++){
                                    res.append(" ");
                                }
                            }
                        }
                    }
                    result.add(res.toString());
                }
                index = lastIndex + 1;
            }
            return result;
        }
    }

Log in to reply
 

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