Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $word = "rates";
- $letters = str_split($word);
- function makeCopy($inputArray) {
- $copyArray = [];
- foreach ($inputArray as $val) {
- array_push($copyArray, $val);
- }
- return $copyArray;
- }
- $found = [];
- function combine($prefix = "", $inputArray) {
- global $found;
- foreach ($inputArray as $key => $currentLetter) {
- $copy = makeCopy($inputArray);
- unset($copy[$key]);
- $copy = array_values($copy);
- $currentWord = $prefix . $currentLetter . implode("", $copy);
- if (!in_array($currentWord, $found)) {
- array_push($found, $currentWord);
- }
- combine($prefix . $currentLetter, $copy);
- }
- }
- foreach ($letters as $key => $currentLetter) {
- $copy = makeCopy($letters);
- unset($copy[$key]);
- $copy = array_values($copy);
- combine($currentLetter, $copy);
- }
- echo print_r($found, true);
Add Comment
Please, Sign In to add comment