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 5da3d65

Browse files
Add solution for Valid Parentheses problem with comprehensive test cases and execution time measurement
1 parent 6bbda81 commit 5da3d65

File tree

2 files changed

+158
-2
lines changed

2 files changed

+158
-2
lines changed

‎README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
- https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
3232
- https://leetcode.com/problems/valid-palindrome/
3333
- https://leetcode.com/problems/merge-sorted-array/
34+
- https://leetcode.com/problems/valid-parentheses/description/
3435

3536
### Medium Problems
3637

@@ -63,15 +64,16 @@
6364
- Rotated Sorted Array
6465
- Palindrome Number
6566
- Factorial
66-
- **Hash Map**
67+
- **Hash Map** (DS)
6768
- 2 Pointers (Two Pointers)
6869
- left and right pointers
6970
- Sliding window
7071
- Prefix sum &&& Suffix sum
7172
- Prefix product &&& Suffix product
72-
- Hash Table
73+
- Hash Table (DS)
7374
- Mark Indices --> By negative value
7475
- Sliding Window
76+
- Stack (DS)
7577

7678
### Algorithms
7779

‎leetcode/20.valid_parentheses.dart

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// https://leetcode.com/problems/valid-parentheses/description/
2+
import 'package:test/test.dart';
3+
4+
void main(List<String> args) {
5+
final stopwatch = Stopwatch()..start();
6+
runTests();
7+
8+
stopwatch.stop();
9+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro s');
10+
}
11+
12+
class Solution {
13+
////! Solved From the first time
14+
bool isValid(String s) {
15+
List<int> brackets = [];
16+
int openCurlyBracket = '{'.codeUnits[0];
17+
int openSquareBracket = '['.codeUnits[0];
18+
int openRoundBracket = '('.codeUnits[0];
19+
20+
int closeCurlyBracket = '}'.codeUnits[0];
21+
int closeSquareBracket = ']'.codeUnits[0];
22+
int closeRoundBracket = ')'.codeUnits[0];
23+
24+
for (final currentBracket in s.codeUnits) {
25+
if (currentBracket == openRoundBracket ||
26+
currentBracket == openSquareBracket ||
27+
currentBracket == openCurlyBracket) {
28+
brackets.add(currentBracket);
29+
30+
/// Make sure that the brackets is not empty
31+
/// before get the last one
32+
} else if (brackets.isNotEmpty) {
33+
int lastAddedBracket = brackets.last;
34+
bool isCloseRight = (lastAddedBracket == openCurlyBracket &&
35+
currentBracket == closeCurlyBracket) ||
36+
(lastAddedBracket == openSquareBracket &&
37+
currentBracket == closeSquareBracket) ||
38+
(lastAddedBracket == openRoundBracket &&
39+
currentBracket == closeRoundBracket);
40+
41+
if (isCloseRight) {
42+
brackets.removeLast();
43+
} else {
44+
return false;
45+
}
46+
} else {
47+
return false;
48+
}
49+
}
50+
51+
return brackets.isEmpty;
52+
}
53+
}
54+
55+
void runTests() {
56+
final Solution s = Solution();
57+
58+
group('Valid Parentheses', () {
59+
// Basic examples from problem statement
60+
test('Example 1: "()" → true', () {
61+
expect(s.isValid("()"), isTrue);
62+
});
63+
64+
test('Example 2: "()[]{}" → true', () {
65+
expect(s.isValid("()[]{}"), isTrue);
66+
});
67+
68+
test('Example 3: "(]" → false', () {
69+
expect(s.isValid("(]"), isFalse);
70+
});
71+
72+
test('Example 4: "([])" → true', () {
73+
expect(s.isValid("([])"), isTrue);
74+
});
75+
76+
// Edge cases
77+
test('Empty string: "" → true', () {
78+
expect(s.isValid(""), isTrue);
79+
});
80+
81+
test('Single opening: "(" → false', () {
82+
expect(s.isValid("("), isFalse);
83+
});
84+
85+
test('Single closing: ")" → false', () {
86+
expect(s.isValid(")"), isFalse);
87+
});
88+
89+
// Nested cases
90+
test('Nested valid: "{[]}" → true', () {
91+
expect(s.isValid("{[]}"), isTrue);
92+
});
93+
94+
test('Nested invalid: "{[}]" → false', () {
95+
expect(s.isValid("{[}]"), isFalse);
96+
});
97+
98+
// Mixed valid cases
99+
test('Mixed valid: "(){}[]" → true', () {
100+
expect(s.isValid("(){}[]"), isTrue);
101+
});
102+
103+
test('Mixed valid complex: "({[]})" → true', () {
104+
expect(s.isValid("({[]})"), isTrue);
105+
});
106+
107+
// Mixed invalid cases
108+
test('Mixed invalid: "(}" → false', () {
109+
expect(s.isValid("(}"), isFalse);
110+
});
111+
112+
test('Mixed invalid complex: "({[}])" → false', () {
113+
expect(s.isValid("({[}])"), isFalse);
114+
});
115+
116+
// Large strings
117+
test('Large valid string', () {
118+
final str = List.filled(5000, '(').join() + List.filled(5000, ')').join();
119+
expect(s.isValid(str), isTrue);
120+
});
121+
122+
test('Large invalid string', () {
123+
final str = List.filled(5000, '(').join() + List.filled(4999, ')').join();
124+
expect(s.isValid(str), isFalse);
125+
});
126+
127+
test('Maximum length invalid', () {
128+
final str = List.generate(10000, (i) => ['(', '[', '{'][i % 3]).join();
129+
expect(s.isValid(str), isFalse);
130+
});
131+
132+
// Random cases
133+
test('Random valid 1: "[({})]()" → true', () {
134+
expect(s.isValid("[({})]()"), isTrue);
135+
});
136+
137+
test('Random invalid 1: "[(])" → false', () {
138+
expect(s.isValid("[(])"), isFalse);
139+
});
140+
141+
// Special cases
142+
test('Only opening brackets: "{[(" → false', () {
143+
expect(s.isValid("{[("), isFalse);
144+
});
145+
146+
test('Only closing brackets: ")}]" → false', () {
147+
expect(s.isValid(")}]"), isFalse);
148+
});
149+
150+
test('Interleaved brackets: "([)]" → false', () {
151+
expect(s.isValid("([)]"), isFalse);
152+
});
153+
});
154+
}

0 commit comments

Comments
(0)

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