Java Program to Implement Word Wrap Problem

This Java program Implements Word Wrap Problem.A Given a sequence of words, and a limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly. Assume that the length of each word is smaller than the line width.

Here is the source code of the Java Program to Implement Word Wrap Problem.The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

public class WordWrapProblem
{
    private static final int INFINITY = Integer.MAX_VALUE;
 
    void solveWordWrap(int l[], int n, int M)
    {
        int extras[][] = new int[n + 1][n + 1];
        int lineCost[][] = new int[n + 1][n + 1];
        int cost[] = new int[n + 1];
        int printSol[] = new int[n + 1];
        int i, j;
 
        for (i = 1; i <= n; i++)
        {
            extras[i][i] = M - l[i - 1];
            for (j = i + 1; j <= n; j++)
            {
                extras[i][j] = extras[i][j - 1] - l[j - 1] - 1;
            }
        }
 
        for (i = 1; i <= n; i++)
        {
            for (j = i; j <= n; j++)
            {
                if (extras[i][j] < 0)
                {
                    lineCost[i][j] = INFINITY;
                } else if (j == n && extras[i][j] >= 0)
                {
                    lineCost[i][j] = 0;
                } else
                    lineCost[i][j] = extras[i][j] * extras[i][j];
            }
        }
        cost[0] = 0;
        for (j = 1; j <= n; j++)
        {
            cost[j] = INFINITY;
            for (i = 1; i <= j; i++)
            {
                if (cost[i - 1] != INFINITY && lineCost[i][j] != INFINITY
                    && (cost[i - 1] + lineCost[i][j] < cost[j]))
                {
                    cost[j] = cost[i - 1] + lineCost[i][j];
                    printSol[j] = i;
                }
            }
        }
        printSolution(printSol, n);
    }
 
    private int printSolution(int p[], int n)
    {
        int k;
        if (p[n] == 1)
        {
            k = 1;
        } else
        {
            k = printSolution(p, p[n] - 1) + 1;
        }
        System.out.println("Line number " + k + " From word no " + p[n] + " to " + n);
        return k;
    }
 
    public static void main(String...arg)
    {
        int l[]  = {3,2,2,5};
        int n = 4;
        int M = 6;
        WordWrapProblem wordWrapProblem = new WordWrapProblem();
        wordWrapProblem.solveWordWrap(l, n, M);
    }	
}
$ javac WordWrapProblem.java
$ java WordWrapProblem
Line number 1 From word no 1 to 1
Line number 2 From word no 2 to 3
Line number 3 From word no 4 to 4

Related posts:

Overview of Spring Boot Dev Tools
Java Program to Implement Sorted Circularly Singly Linked List
The Spring @Controller and @RestController Annotations
A Guide to Apache Commons Collections CollectionUtils
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Java Program to Implement Hash Tables Chaining with Binary Trees
Hướng dẫn sử dụng String Format trong Java
Java Program to Implement Maximum Length Chain of Pairs
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Java Program to Implement TreeMap API
Guide to Guava Multimap
Guava – Join and Split Collections
Java Program to Perform Uniform Binary Search
Checked and Unchecked Exceptions in Java
Java Program to Find Transitive Closure of a Graph
Composition, Aggregation, and Association in Java
Giới thiệu Google Guice – Binding
Hướng dẫn sử dụng Java Generics
Java Deep Learning Essentials - Yusuke Sugomori
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Spring Webflux with Kotlin
ETL with Spring Cloud Data Flow
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Implement Interpolation Search Algorithm
Spring Cloud – Tracing Services with Zipkin
Set Interface trong Java
Testing an OAuth Secured API with Spring MVC
Guide to the Fork/Join Framework in Java
Check if a String is a Palindrome in Java
Java Program to Implement Merge Sort on n Numbers Without tail-recursion
Convert String to Byte Array and Reverse in Java
Getting a File’s Mime Type in Java