-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.php
52 lines (49 loc) · 1.27 KB
/
Solution.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
<?php
namespace leetcode\g0001_0100\s0032_longest_valid_parentheses;
// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming #Stack #Big_O_Time_O(n)_Space_O(1)
// #2023_12_09_Time_8_ms_(67.65%)_Space_19.2_MB_(85.29%)
class Solution {
/**
* @param String $s
* @return Integer
*/
public function longestValidParentheses($s) {
$max = 0;
$left = 0;
$right = 0;
$n = strlen($s);
for ($i = 0; $i < $n; $i++) {
$ch = $s[$i];
if ($ch == '(') {
$left++;
} else {
$right++;
}
if ($right > $left) {
$left = 0;
$right = 0;
}
if ($left == $right) {
$max = max($max, $left + $right);
}
}
$left = 0;
$right = 0;
for ($i = $n - 1; $i >= 0; $i--) {
$ch = $s[$i];
if ($ch == '(') {
$left++;
} else {
$right++;
}
if ($left > $right) {
$left = 0;
$right = 0;
}
if ($left == $right) {
$max = max($max, $left + $right);
}
}
return $max;
}
}