PHP Anagram Algorithm

I am completely new to PHP. Today I just have a problem that I can’t figure out how to solve it, even after google searching and digging SOF. This is the Anagram algorithm.

So, I understand the problem here: when the user enters a string, I split it and compare it with my library (this array), then I will have to attach it to the 2-3 characters ... etc. compare again, exactly where I am stuck now, I do not know how to join the elements of the array.

Here is the code that I am implementing, as well as a sample dictionary.

I have a makeshift dictionary with these elements in the $ dict array. And I have a form for a user to enter a string, the entered string will be passed to the code below and declared as $ anagram. I need to split the string entered for comparison with my dictionary. But I don’t know how to join them by comparing 2 letters, 3 letters ... etc. Etc., in the dictionary.

<?php

$dict = array(
'abde',
'des',
'klajsd',
'ksj',
'hat',
'good',
'book',
'puzzle',
'local',
'php',
'e');

$anagram = $_POST['anagram'];
//change to lowercase
$anagram = strtolower($anagram);

//split the string
$test = str_split($anagram);

//compare with $dict for the first split without joining
for ($i=0; $i<strlen($anagram); $i++) {
    if ($test[$i]==$dict[$i]) {
        echo $test[$i]."<br />";
    }
}

//problem: how to join elements of the array in the loops
//like user inputs "hellodes"
//after echo "e", how to join the elements like: h-e,h-l,h-l,h-o,h-d,h-e,h-s
//and then h-e-l,h-e-l,h-e-o...etc...
?>

I hope to get the algorithm as simple as possible because I'm completely new. And I'm sorry because my English is not so good. Best regards, Hiem Nguyen.

+3
source share
5 answers

(I add this as a separate answer, as this is a different way to solve the problem than I mentioned in my first release)

, , ; , , .

, . , , - ; , . CAT, , 37 * 5 * 3 510. , , .

, , , .

<?php

function factorise($word)
{
    // Take a number, split it into individual letters, and multiply those values together
    // So long as both words use the same value, you can amend the ordering of the factors 
    // as you like

    $factors = array("e" => 2, "t" => 3, "a" => 5, "o" => 7, "i" => 11,
        "n" => 13, "s" => 17, "h" => 19, "r" => 23, "d" => 29,
        "l" => 31, "c" => 37, "u" => 41, "m" => 43, "w" => 47,
        "f" => 53, "g" => 59, "y" => 61, "p" => 67, "b" => 71,
        "v" => 73, "k" => 79, "j" => 83, "x" => 89, "q" => 97,
        "z" => 101);

    $total = 1;

    $letters = str_split($word);

    foreach ($letters as $thisLetter) {
        if (isset($factors[$thisLetter])) {
            // This will skip any non-alphanumeric characters.
            $total *= $factors[$thisLetter];
        }
    }

    return $total;
}

$searchWord = "hasted";

$dict = array("abde", "des", "klajsd", "ksj", "hat", "hats");

$searchWordFactor = factorise($searchWord);

foreach ($dict as $thisWord) {
    // Factorise each word that we're looking for
    // If the word we've just factored is an exact divisor of the target word, then all the 
    // letters in that word are also present in the target word
    // If you want to do an exact anagram, then check that the two totals are equal

    $dictWordFactor = factorise($thisWord);

    if (($searchWordFactor % $dictWordFactor) == 0) {
        print ($thisWord . " is an anagram of " . $searchWord . "<br/>");
    }
}

, , - , . , :

SELECT word FROM dictionary WHERE wordFactor='$factorOfThisWord'
+19

, ; , :

get array of letters in my anagram
for each word in the dictionary
    get array of letters in this word
    for each letter in my anagram
        is this letter also in the word?
            if no, move on to the next word
    if we get here, it an anagram

, - , , ( , ); , , , ( "aa" "a", )

+2

, . , ?

- 26 . , [letter - 'a'] ( -, php) 1 .

array_dict = 0... 25, array [i] == array_dict [i]. , . , array_dict .

. , / - , . , , ( #, , php)

Dictionary<string, List<string>>

and pre-process the dictionary by sorting each word, viewing it in the dictionary, if the list does not exist, create it and in any case add the word to the list. Then, when the user enters a word, you can sort it and return the dictionary [sortedword] as the result - all anagrams found mostly in constant time (nlogn by the length of the input line, but constant by the size of the dictionary).

0
source
$dictionary = array("kayak");

$anagram = "kayak";

$anagramSorted = sortString($anagram);


foreach ($dictionary as $word)
{
    $wordSorted = sortString($word);
    if ($wordSorted == $anagramSorted)
    {
       echo 'true';
    }
}

function sortString($s)
{
    $chars = array();
    $length = strlen($s);
    for ($i=0;$i<$length;$i++)
    {
       $chars[] = $s[$i];
    }
    sort($chars);

    return implode("",$chars);
}
0
source

Try the drag and drop function?

str_shuffle ( string $str )

Here are some psuedocode:

Get random string from array
store string copy (Not shuffled)
string shuffle another copy
echo shuffled string
get users guess
parse guess (Remove illegal characters)
if parsed guess = string
    reward
else
    ?let user try again?
0
source

All Articles