# UVA 12478 – Hardest Problem Ever (Easy)

So this was an interesting problem. You could have technically solved it manually but that would have taken too much time. If you didn’t mind at most 7 WA’s you could have just tried all 8 solutions one at a time and gotten it correct. I did it the long way; making my program solve it for me.

I used this string normalization technique I was ‘taught’ in college. It’s a neat little trick that I seem to use every chance I get. Pretty easy solution once you know you can map all permutations of a string to just one string!

```import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
*
* @author Sanchit M. Bhatnagar
* @see http://uhunt.felix-halim.net/id/74004
*
*/
public class P12478 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);

String[] grid = new String[] { "OBIDAIBKR", "RKAULHISP", "SADIYANNO", "HEISAWHIA", "IRAKIBULS", "MFBINTRNO", "UTOYZIFAH", "LEBSYNUNE", "EMOTIONNAL" };

String[] originalNames = new String[] { "RAKIBUL", "ANINDYA", "MOSHIUR", "SHIPLU", "KABIR", "SUNNY", "OBAIDA", "WASI" };

String[] names = new String[originalNames.length];
int[] count = new int[names.length];

for (int i = 0; i &lt; names.length; i++) {
names[i] = normalize(originalNames[i]);
}

// Check Horizontally
for (int zz = 0; zz &lt; 9; zz++) {
for (int i = 0; i &lt; 9; i++) {
for (int j = i + 4; j &lt;= 9; j++) { //Shortest name is 4 characters so we are checking for sub-strings of at least that length. We could also have capped the sub-string to at most 7 characters based on the names provided.
String substring = normalize(grid[zz].substring(i, j));
for (int k = 0; k &lt; names.length; k++) {
if (substring.equals(names[k])) {
count[k]++;
}
}
}
}
}

// Check Vertically
String[] grid2 = new String[grid.length];
for (int j = 0; j &lt; 9; j++) {
char[] tmp = new char[9];
for (int i = 0; i &lt; 9; i++) {
tmp[i] = grid[i].charAt(j);
}
grid2[j] = new String(tmp);
}

for (int zz = 0; zz &lt; 9; zz++) {
for (int i = 0; i &lt; 9; i++) {
for (int j = i + 4; j &lt;= 9; j++) {
String substring = normalize(grid2[zz].substring(i, j));
for (int k = 0; k &lt; names.length; k++) {
if (substring.equals(names[k])) {
count[k]++;
}
}
}
}
}

for (int i = 0; i &lt; count.length; i++) {
if (count[i] == 2) {
out.println(originalNames[i]);
}
}

out.close();
sc.close();
}

// Normalizes all strings
private static String normalize(String word) {
char[] list = word.toCharArray();
Arrays.sort(list);
return new String(list);
}
}
```

Edit: Further explanation. I’ve also updated the code to avoid the IndexOutOfBounds exception I was strangely using a try-catch for.

Firstly let us cover the normalize function. As you can tell by the code, it takes a word, splits it into a character array, sorts the character array and then converts it back into a string. Let us take some examples so that this is clear.

Take a word, let’s say: apple once you run it through the normalize function it will become aelpp. The same will happen for all permutations of apple (I won’t list them all) for example aplpe, ppael, leapp, etc. This allows us to quickly check if a particular permutation of a word is in the grid.

If this part is clear then we can just move on to the checking part.

We will go through the grid one row at a time (the code with the comment stating horizontal). Find all substrings within that line, normalize them and then compare them with the names array. If they are in the array then we increment a counter (named count). We do the same thing for the grid vertically.

## 2 thoughts on “UVA 12478 – Hardest Problem Ever (Easy)”

1. Vatsal says:

please explain this program a bit to newbies like me

1. I explained a bit further. Let me know if that makes sense. Feel free to ask me again if this is unclear.

This site uses Akismet to reduce spam. Learn how your comment data is processed.