14
1
Consider how a word could be arranged on an arbitrarily large Boggle grid if the rule about not using the same letter cube more than once is ignored. Also assume that you have an unlimited number of letter cubes (with all letters present), and Qu
is just Q
.
The word MISSISSIPPI
could be arranged using only 6 cubes. Here is one possible arrangement:
S
MIS
PP
Starting at the M
we repeatedly take any step horizontally, vertically, or diagonally until the entire word is spelled out.
Surprisingly, a longer phrase like AMANAPLANACANALPANAMA
also only needs 6 cubes:
MAN
PLC
However, the minimum number of cubes needed for longer, more complex strings is not always obvious.
Challenge
Write a program that takes in a string and arranges it in this Boggle-like fashion such that the minimum number of cubes are used. (The dimensions of the resulting grid and the number of empty cells are irrelevant.)
Assume you have have an unlimited number of cubes for each printable ASCII character except space (hex codes 21 to 7E), since it's used as an empty grid cell. Only printable ASCII strings (without spaces) will be input.
Input should be taken from stdin or the command line. Output should go to stdout (or closest alternative).
Leading or trailing newlines and spaces in the output are fine (but hopefully there aren't an inordinate amount).
The search space blows up exponentially as the string gets longer, but you are not required to attempt to make your algorithm efficient (though that would be nice :) ). This is code-golf, so the shortest solution in bytes wins.
Example
If the input were Oklahoma!
(8 character minimum), these would all be valid outputs because the all have exactly 8 filled grid cells and they follow the (revised) Boggle reading pattern:
Oklaho
!m
or
!
Oamo
klh
or
lkO
!amo
h
etc.
4This sounds like a tough optimisation problem. I think it would have made a nice code challenge. – Martin Ender – 2014-09-17T11:31:09.933
1I can't edit because there's a pending edit from a low-rep user, but Mississippi has two double-s. – Peter Taylor – 2014-09-17T11:54:58.427
What about case sensitivity? – proud haskeller – 2014-09-17T11:56:43.157
1@proudhaskeller I'm sure the input is case insensitive, because: 1) the input is just any printable ASCII 2) the OP didn't mention otherwise 3) The oklahoma example wouldn't be minimal if the input was case insensitive. – Martin Ender – 2014-09-17T12:15:21.020
@MartinBüttner I think you meant *case sensitive* – Ypnypn – 2014-09-17T13:40:28.507
@Ypnypn Absolutely! (because otherwise my 3rd point would make sense ^^) Thanks ;) – Martin Ender – 2014-09-17T13:41:44.683
@proudhaskeller Uppercase ans lowercase are distinct for the exact reasons MartinBüttner supplied. – Calvin's Hobbies – 2014-09-17T19:08:13.133
Wow how many replies for suck a simple question :) – proud haskeller – 2014-09-17T19:09:34.263