Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 712d70f

Browse files
Add solution for Longest Valid Parentheses problem with comprehensive test cases
1 parent 16a6332 commit 712d70f

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
### Hard Problems
5757

5858
- https://leetcode.com/problems/first-missing-positive/description/
59-
-
59+
- Very Hard with idea: https://leetcode.com/problems/longest-valid-parentheses/description/
6060

6161
### Topics
6262

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Very Hard https://leetcode.com/problems/longest-valid-parentheses/description/
2+
3+
import 'package:test/test.dart';
4+
5+
void main(List<String> args) {
6+
final stopwatch = Stopwatch()..start();
7+
runTests();
8+
9+
// print(Solution().divide(-17, 5));
10+
stopwatch.stop();
11+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec');
12+
}
13+
14+
class Solution {
15+
int longestValidParentheses(String s) {
16+
final List<int> stack = [-1];
17+
int lengthResult = 0;
18+
int maxLength = 0;
19+
final chars = s.split('').toList();
20+
for (var i = 0; i < s.length; i++) {
21+
final char = chars[i];
22+
23+
/// if it is close
24+
if (char == ')') {
25+
stack.removeLast();
26+
if (stack.isNotEmpty) {
27+
lengthResult = i - stack.last;
28+
// stack.removeLast();
29+
} else {
30+
stack.add(i);
31+
}
32+
}
33+
34+
/// It is open
35+
else if (char == '(') {
36+
stack.add(i);
37+
}
38+
39+
if (maxLength < lengthResult) {
40+
maxLength = lengthResult;
41+
}
42+
}
43+
44+
return maxLength;
45+
}
46+
}
47+
48+
void runTests() {
49+
final Solution s = Solution();
50+
51+
group('Longest Valid Parentheses', () {
52+
// Basic examples from problem statement
53+
test('Example 1: "(()" → 2', () {
54+
expect(s.longestValidParentheses("(()"), equals(2));
55+
});
56+
57+
test('Example 2: ")()())" → 4', () {
58+
expect(s.longestValidParentheses(")()())"), equals(4));
59+
});
60+
61+
test('Example 3: "" → 0', () {
62+
expect(s.longestValidParentheses(""), equals(0));
63+
});
64+
65+
// Edge cases
66+
test('Single opening: "(" → 0', () {
67+
expect(s.longestValidParentheses("("), equals(0));
68+
});
69+
70+
test('Single closing: ")" → 0', () {
71+
expect(s.longestValidParentheses(")"), equals(0));
72+
});
73+
74+
// Simple valid cases
75+
test('Simple valid: "()" → 2', () {
76+
expect(s.longestValidParentheses("()"), equals(2));
77+
});
78+
79+
test('Nested valid: "(()())" → 6', () {
80+
expect(s.longestValidParentheses("(()())"), equals(6));
81+
});
82+
83+
// No valid pairs
84+
test('No valid pairs: ")))(((" → 0', () {
85+
expect(s.longestValidParentheses(")))((("), equals(0));
86+
});
87+
88+
// Mixed cases
89+
test('Mixed valid/invalid: "()(()" → 2', () {
90+
expect(s.longestValidParentheses("()(()"), equals(2));
91+
});
92+
93+
test('Complex mixed: ")(()())(" → 6', () {
94+
expect(s.longestValidParentheses(")(()())("), equals(6));
95+
});
96+
97+
// Maximum length cases
98+
test('Max length all valid: "(((...)))"', () {
99+
final input = "(" * 15000 + ")" * 15000;
100+
expect(s.longestValidParentheses(input), equals(30000));
101+
});
102+
103+
// Alternating pattern
104+
test('Alternating pattern: "()()()" → 6', () {
105+
expect(s.longestValidParentheses("()()()"), equals(6));
106+
});
107+
108+
// Complex nested
109+
test('Complex nested: "((()()()))" → 10', () {
110+
expect(s.longestValidParentheses("((()()()))"), equals(10));
111+
});
112+
113+
// Partial valid at end
114+
test('Partial valid at end: "(()()))" → 6', () {
115+
expect(s.longestValidParentheses("(()()))"), equals(6));
116+
});
117+
118+
// Random cases
119+
test('Random case 1: "()(())" → 6', () {
120+
expect(s.longestValidParentheses("()(())"), equals(6));
121+
});
122+
123+
test('Random case 2: "((()))())" → 8', () {
124+
expect(s.longestValidParentheses("((()))())"), equals(8));
125+
});
126+
});
127+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /