1
+ <?php
2
+ /**
3
+ * MIT License
4
+ *
5
+ * Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in all
15
+ * copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ * SOFTWARE.
24
+ */
25
+
26
+ namespace doganoo \PHPAlgorithms \Algorithm \Various ;
27
+
28
+ use doganoo \PHPUtil \Util \NumberUtil ;
29
+ use doganoo \PHPUtil \Util \StringUtil ;
30
+
31
+ /**
32
+ * Class Permutation
33
+ *
34
+ * @package doganoo\PHPAlgorithms\Algorithm\Various
35
+ */
36
+ class Permutation {
37
+ /**
38
+ * returns all permutations of a given string
39
+ *
40
+ * @param string $string
41
+ * @return array
42
+ */
43
+ public function stringPermutations (string $ string ): array {
44
+ $ result = [];
45
+ $ strLen = \strlen ($ string );
46
+ if (0 === $ strLen ) {
47
+ return $ result ;
48
+ }
49
+ if (1 === $ string ) {
50
+ $ result [] = $ string ;
51
+ return $ result ;
52
+ }
53
+
54
+ $ array = StringUtil::stringToArray ($ string );
55
+ $ result = $ this ->permute ($ array , "" , $ result );
56
+ return $ result ;
57
+ }
58
+
59
+ /**
60
+ * returns all permutations of an given array of objects
61
+ *
62
+ * @param array $objects
63
+ * @param $prefix
64
+ * @param array $result
65
+ * @return array
66
+ */
67
+ private function permute (array $ objects , $ prefix , array $ result ) {
68
+ $ length = \count ($ objects );
69
+ if (0 === $ length ) {
70
+ $ result [] = $ prefix ;
71
+ } else {
72
+ for ($ i = 0 ; $ i < $ length ; $ i ++) {
73
+ $ newObjects = \array_merge (
74
+ \array_slice ($ objects , 0 , $ i ),
75
+ \array_slice ($ objects , $ i + 1 )
76
+ );
77
+ $ newPrefix = $ prefix . $ objects [$ i ];
78
+
79
+ $ result = $ this ->permute ($ newObjects , $ newPrefix , $ result );
80
+ }
81
+ }
82
+ return $ result ;
83
+ }
84
+
85
+ /**
86
+ * returns all permutations of a given string
87
+ *
88
+ * @param int $number
89
+ * @return array
90
+ */
91
+ public function numberPermutations (int $ number ) {
92
+ $ result = [];
93
+ $ array = NumberUtil::intToArray ($ number );
94
+ $ result = $ this ->permute ($ array , "" , $ result );
95
+ foreach ($ result as &$ item ) {
96
+ \settype ($ item , "integer " );
97
+ }
98
+ return $ result ;
99
+ }
100
+ }
0 commit comments