Guest User

Untitled

a guest
Oct 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. <?php
  2. $word = "rates";
  3. $letters = str_split($word);
  4.  
  5. function makeCopy($inputArray) {
  6. $copyArray = [];
  7.  
  8. foreach ($inputArray as $val) {
  9. array_push($copyArray, $val);
  10. }
  11.  
  12. return $copyArray;
  13. }
  14.  
  15. $found = [];
  16.  
  17. function combine($prefix = "", $inputArray) {
  18. global $found;
  19.  
  20. foreach ($inputArray as $key => $currentLetter) {
  21. $copy = makeCopy($inputArray);
  22. unset($copy[$key]);
  23. $copy = array_values($copy);
  24.  
  25. $currentWord = $prefix . $currentLetter . implode("", $copy);
  26.  
  27. if (!in_array($currentWord, $found)) {
  28. array_push($found, $currentWord);
  29. }
  30.  
  31. combine($prefix . $currentLetter, $copy);
  32. }
  33. }
  34.  
  35. foreach ($letters as $key => $currentLetter) {
  36. $copy = makeCopy($letters);
  37. unset($copy[$key]);
  38. $copy = array_values($copy);
  39.  
  40. combine($currentLetter, $copy);
  41. }
  42.  
  43. echo print_r($found, true);
Add Comment
Please, Sign In to add comment