## Thursday, January 29, 2015

### Justifier

Problem:

 TopCoder Problem Statement - Justifier Single Round Match 164 Round 1 - Division II, Level One

Overview:

Right justify a set of Strings so that their edges line up.

Java Source:
```01: public class Justifier {
02:
03:     public String[] justify(String[] textIn) {
04:
05:         int longest = 0;
06:
07:         // Find the longest string.
08:         for (int i = 0; i < textIn.length; i++)  {
09:             longest = Math.max(longest, textIn[i].length());
10:         }
11:
12:         String[] result = new String[textIn.length];
13:
14:         // Loop through each of the strings.
15:         for (int i = 0; i < textIn.length; i++)  {
16:             StringBuilder s = new StringBuilder(textIn[i]);
17:
18:             // Insert spaces at the beginning until length == longest.
19:             while (s.length() < longest)  {
20:                 s.insert(0, " ");
21:             }
22:
23:             result[i] = s.toString();
24:         }
25:
26:
27:         return result;
28:     }
29:
30: }
```
Notes:

We can solve this easily in two steps. First, loop through all the Strings in textIn and make note of the longest. Then loop through each of the Strings again, and pad them with spaces at the beginning until their length matches the longest found in the first step.