Tuesday, February 3, 2015

Text Justification LeetCode

X stands for the space.
ThisXXisXanXexample.

currList only contains the non-first words.
So in the currList it will be "Xis" "Xan" "Xexample".

The example showing in the leetcode is not accurate. We should put spaces in front of the first word.


public List<String> fullJustify(String[] words, int L) {
        List<String> currList = new ArrayList<String>();
        List<String> res = new ArrayList<String>();
        int counts=0, len=0;
        StringBuilder sb = new StringBuilder();
     
        while (counts < words.length) {
            //1 put the first word into the line
            sb.setLength(0);
            sb.append(words[counts]);
            currList.clear();
            len = words[counts].length();
            counts++;
         
            //2. Handle the rest of the words for each line
            while (counts<words.length && len+1+words[counts].length()<=L) {
                currList.add(" " + words[counts]);
                len += words[counts].length()+1;
                counts++;
            }
         
            //3. Calculate the number of spaces we need and insert into the currList
            if (counts<words.length && currList.size()>0) {
                int numSpaces = L-len;
                int avg = numSpaces/currList.size();
                int rem = numSpaces%currList.size();
                for (int i=0; i<currList.size(); i++) {
                    appendSpace(sb, i<rem ? avg+1 : avg);
                    sb.append(currList.get(i));
                }
            } else {
                //4. Special case for the last line
                for (int j=0; j<currList.size(); j++) {
                    sb.append(currList.get(j));
                }
                    appendSpace(sb, L-len);
            }
                res.add(sb.toString());
        }
        return res;
    }
 
    private void appendSpace(StringBuilder sb, int n) {
        for (int i=0; i<n; i++) {
            sb.append(" ");
        }
    }

No comments:

Post a Comment