MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / fullJustify

Method fullJustify

TextJustification.java:2–21  ·  view source on GitHub ↗
(String[] words, int maxWidth)

Source from the content-addressed store, hash-verified

1class Solution {
2 public List<String> fullJustify(String[] words, int maxWidth) {
3 List<String> result = new ArrayList<String>();
4 int word=0;
5 while(word<words.length)
6 {
7 int j=word-1;
8 int characters=0;
9// Max words that can be adjusted in one line, that is :
10// cuurent length (words[j+1].length()) + total characters seen so far for this line (characters) + the spaces between each pair of words (j+1-word)
11 while(j+1<words.length && characters+words[j+1].length() + j+1-word<=maxWidth)
12 {
13 j++;
14 characters+=words[j].length();
15 }
16 // Adding each line
17 result.add(line(words,word,j,characters,maxWidth));
18 word=j+1;
19 }
20 return result;
21 }
22 public String line(String words[],int start,int end, int Linelen,int max)
23 {
24 StringBuilder a = new StringBuilder();

Callers

nothing calls this directly

Calls 2

lineMethod · 0.95
addMethod · 0.45

Tested by

no test coverage detected