-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathque3.php
More file actions
67 lines (51 loc) · 1.25 KB
/
que3.php
File metadata and controls
67 lines (51 loc) · 1.25 KB
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
60
61
62
63
64
65
66
67
<?php
/**
* 3.实现函数,判断一个字符串是不是另外一个字符串的“变形”(包含的字符及每个字符数量相同,仅排列不同)。例如:
"good"
"dogo"
应返回true,
"car"
"cdr"
应返回false.
*/
class Solution
{
function checkWord($a, $b)
{
//长度不同,肯定为false
if(strlen($a) !== strlen($b)){
return false;
}
$a = str_split($a);
$b = str_split($b);
//两个字符串统一排序,此处为体现题意考察,采用冒泡排序,否则可以数组函数排序
$a = $this->doSort($a);
$b = $this->doSort($b);
if($a === $b){
return true;
}
return false;
}
function doSort($arr)
{
$len = count($arr);
if($len <= 1){
return $arr;
}
for ($i = 0; $i < $len; $i++){
for ($j = $i + 1; $j < $len; $j++){
if($arr[$i] > $arr[$j]){
$tmp = $arr[$i];
$arr[$i] = $arr[$j];
$arr[$j] = $tmp;
}
}
}
return $arr;
}
}
//test
$a = "good";
$b = "odgo";
$model = new Solution();
var_dump($model->checkWord($a, $b));