Java Accepted solution


  • 0
    D
    public class Solution {
        public List<String> fullJustify(String[] words, int maxWidth) {
            int idx = 0, L = maxWidth;
            int start = 0, end = 0;
            List<String> ret = new ArrayList<>();
            while (start < words.length) {
                if (words[start].length() == L) {
                    ret.add(words[start]);
                    start += 1;
                } else {
                    int len = words[start].length(), i;
                    for (i = start + 1; i < words.length; i++) {
                        len += 1 + words[i].length();
                        if (len > L) 
                        	break;
                    }
                    if (i == words.length && len <= L) {
                        end = words.length - 1;
                        StringBuffer temp = new StringBuffer(words[start]);
                        for (int j = start + 1; j <= end; j++) {
                            temp.append(" " + words[j]);
                        }
                        if (temp.length() == L) {
                            ret.add(temp.toString());
                        } else {
                            char[] rightSpaces = new char[L - temp.length()];
                            Arrays.fill(rightSpaces, ' ');
                            temp.append(rightSpaces);
                            ret.add(temp.toString());
                        }
                    } else if (len > L) {
                        end = i - 1;
                        if (end == start) {
                        	StringBuffer temp = new StringBuffer(words[start]);
                            char[] rightSpaces = new char[L - temp.length()];
                            Arrays.fill(rightSpaces, ' ');
                            temp.append(rightSpaces);
                            ret.add(temp.toString());
                        } else {
                            int spaces_left = L, j;
                            for (j = start; j <= end; j++)
                                spaces_left -= words[j].length();
    	                    int each_at_least = spaces_left / (end - start);
    	                    StringBuffer[] spaces = new StringBuffer[end - start];
    	                    for (j = 0; j < end - start; j++) {
    	                        char[] ca = new char[each_at_least];
    	                        Arrays.fill(ca, ' ');
    	                        spaces[j] = new StringBuffer(new String(ca));
    	                    }
    	                    spaces_left = spaces_left % (end - start);
    	                    for (j = 0; j < end-start && spaces_left > 0; j++) {
    	                        spaces[j].append(' ');
    	                        spaces_left--;
    	                    }
    	                    StringBuffer temp = new StringBuffer(words[start]);
    	                    for (j = 0; j < end-start; j++) {
    	                        temp.append(spaces[j]).append(words[start+1+j]);
    	                    }
    	                    ret.add(temp.toString());
                        }
                    }
                    start = end + 1;
                }
            }
            return ret;
        }
    }

Log in to reply
 

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