0

I know there are lots of questions about unscrambling the words but this one is different. I'll explain after the sample code.

Sample Code

//Function
function unscramble($input){
    $output = [];
    $input = strtolower($input);
    $words = file_get_contents('http://localhost/words.txt');
    
    for($i=strlen($input);$i>0;$i=$i-1) {
        $letters = count_chars(substr($input, 0, $i),1);
        foreach (explode(' ', $words) as $word) {
          if ($letters == count_chars($word,1))
            $output[] = $word;
        }
    }
    return $output;
}
//Usage
print_r(unscramble('word'));

Well, the above code works as expected and returns the following words:

Array
(
    [0] => word
    [1] => row
    [2] => ow
    [3] => wo
)

The word "do" could've been an addition but it wasn't in the output since our function deletes characters one by one and generates the following words to search from the dictionary:

word
wor
wo
w

The only way it is possible is to generate every possible word from a given word. For example, the following list contains every possibility of a "word":

d
o
r
w
do
dr
dw
or
ow
rw
dor
dow
drw
orw
dorw

To achieve this, I need to modify the sample code. Right now it deletes the last character and stops once there's a single character left. I wonder how this site doing it maybe it needs a function to randomize words or some other logic. Once we have our list we can search it through the words file.

Any help is appreciated, thanks!

James
  • 1

0 Answers0