-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCP.php
More file actions
37 lines (36 loc) · 935 Bytes
/
LCP.php
File metadata and controls
37 lines (36 loc) · 935 Bytes
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
<?php
class Solution
{
/**
* @param String[] $strs
* @return String
*/
function longestCommonPrefix($strs)
{
$array_count = count($strs);
if ($array_count == 0) {
return "";
}
$temp_str = $strs[0];
$prefix = "";
for ($i = 0; $i < strlen($temp_str); $i++) {
for ($j = 1; $j < count($strs); $j++) {
$str_now = $strs[$j];
if (strlen($str_now) == 0) {
return "";
} else {
if ($temp_str[$i] == $str_now[$i]) {
continue;
} else {
return $prefix;
}
}
}
$prefix .= $temp_str[$i];
}
return $prefix;
}
}
$strs = array("flower", "fxow", "flight");
$obj = new Solution();
echo $obj->longestCommonPrefix($strs);