I
I
Igor Kaspersky2015-11-26 09:22:36
PHP
Igor Kaspersky, 2015-11-26 09:22:36

Given 2 words, determine whether it is possible to make a 2nd word from the 1st word, provided that each letter from string 1 can be used only once?

hello, i can't figure out what to do next

<form action="" method="post">
        <br>
        <input type="text" name="first" autofocus="" placeholder="Первое слово">
        
        <input type="text" name="second" placeholder="Второе слово">
        
        <input type="submit" name="">
        </form>

How to further check the word to match?
<?php
            $first = $_POST["first"];
            $lenghtF = iconv_strlen($first);
            
            $second = $_POST["second"];
            $lenghtS = iconv_strlen($second);
            
            empty($first) && empty($second) ? exit : '';
            
            for ($i=0; $i <= $lenghtF; $i++ )
            {
                echo $first[$i];
            }
            
            echo "<br /><br />";
            
            for ($i=0; $i <= $lenghtS; $i++ )
            {
                echo $second[$i];
            }
            
            echo "<br /><br />";
            
            for ($i = 0; $i <= $lenghtF; $i++) 
            {
                for($j=0; $j <= $lenghtS; $j++)
                {
                    if ($first[$i] == $second[$j])
                        printf("Буква из слова '%s'подходит к слову '%s' <br />",$first,$second);
                    else 
                    printf("Не подходит<br />");
                }
                // $smb = strpos($first, $second[$i]); // ищем в "почве" нужный нам символ
                // $smb ? $check .= $second[$i] : print("$second[$i] !-> $first<br />"); // создаем из символов слово (для проверки)
                // $first[$smb] = ''; // удаляем символ, который нашли из нашей "почвы"
            }
            
            strcasecmp($second, $check) != 0 ? print("слово " . $second . " не подходит") : print("слово " . $second . " подходит");
        ?>

The code in the comments is not needed, it is not mine.
I thought if you check each letter in cycles like this, then it will be possible to compare, but it turns out to be useless, how to proceed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2015-11-26
@HHabar

Given 2 words, determine whether it is possible to make 2 words from 1, provided that each letter from string 1 can be used only once?
If this is read as "determine whether the first word can be made into a second", then the algorithm is simple:
In the third paragraph, I can be mistaken, it is difficult to understand by eye which solution is correct, and even with such an incomprehensible wording. But the point is that your task can be reduced to comparing arrays. And how exactly to compare them depends on the task, I recommend that you come up with hypotheses and test them, a very useful exercise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question