/
784. 字母大小写全排列.php
59 lines (52 loc) · 1.46 KB
/
784. 字母大小写全排列.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* Created by PhpStorm
* User: jtahstu
* Time: 2022/6/20 16:42
* Desc: 784. 字母大小写全排列
* https://leetcode.cn/problems/letter-case-permutation/
* 给定一个字符串 s ,通过将字符串 s 中的每个字母转变大小写,我们可以获得一个新的字符串。
* 返回 所有可能得到的字符串集合 。以 任意顺序 返回输出。
*/
class Solution
{
public $res = [];
public $len = 0;
public $s = [];
/**
* @param String $s
* @return String[]
*/
function letterCasePermutation($s)
{
$this->s = $s;
$this->len = strlen($s);
$this->dfs(0, []);
return $this->res;
}
public function dfs($i, $t)
{
if ($i == $this->len) {
$this->res[] = implode('', $t);
return;
}
if (!is_numeric($this->s[$i])) {
$t[] = strtolower($this->s[$i]);;
$this->dfs($i + 1, $t);
array_pop($t);
$t[] = strtoupper($this->s[$i]);
$this->dfs($i + 1, $t);
array_pop($t);
} else {
$t[] = $this->s[$i];
$this->dfs($i + 1, $t);
}
}
}
print_r((new Solution)->letterCasePermutation("a1b2"));
//["a1b2", "a1B2", "A1b2", "A1B2"]
/**
* 执行用时:16 ms, 在所有 PHP 提交中击败了54.55%的用户
* 内存消耗:20.4 MB, 在所有 PHP 提交中击败了18.18%的用户
* 通过测试用例:63 / 63
*/