Question
Hi, I have a code here and I wanted to know if it was implemented correctly. The language is Java and the Problem is below
Hi, I have a code here and I wanted to know if it was implemented correctly. The language is Java and the Problem is below my code.
import java.util.ArrayList;
import java.util.List;
public class ReArrange {
public List
ArrayList
if (words == null || words.length == 0) {
return result;
}
int count=0;
int last=0;
for (int i=0;i count = count + words[i].length(); if (count+i-last > maxWidth) { int wordsLength = count - words[i].length(); int spaceLength = maxWidth - wordsLength; int eachLen = 1; int extraLen = 0; if (i-last-1 > 0) { eachLen = spaceLength /(i-last-1); extraLen = spaceLength % (i-last-1); } StringBuilder sb = new StringBuilder(); for (int k=last;k sb.append(words[k]); int ce = 0; while (ce sb.append(" "); ce++; } if (extraLen > 0) { sb.append(" "); eachLen--; } } sb.append(words[i-1]); while (sb.length() sb.append(" "); } result.add(sb.toString()); last = i; count = words[i].length(); } } StringBuilder sb = new StringBuilder(); for (int i=last;i count = count + words[i].length(); sb.append(words[i] + " "); } sb.append(words[words.length-1]); while (sb.length() sb.append(" "); } result.add(sb.toString()); return result; } public static void main(String[] args) { ReArrange t = new ReArrange(); String[] words1 = {"This", "is", "an", "example", "of", "text", "justification."}; String[] words2 = {"What","must","be","acknowledgment","shall","be"}; String[] words3 = {"Science","is","what","we","understand","well","enough","to","explain", "to","a","computer.","Art","is","everything","else","we","do"}; System.out.println(t.fullJustify(words1, 16)); System.out.println(t.fullJustify(words2, 16)); System.out.println(t.fullJustify(words3, 20)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started