Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1956 from laitooo/0020-valid-parenthes…
Browse files Browse the repository at this point in the history
…es.dart

Create: 0020-Valid-Parentheses.dart
  • Loading branch information
tahsintunan authored Jan 9, 2023
2 parents 53778be + 7e49102 commit 1609f47
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions dart/0020-valid-parentheses.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
bool isValid(String s) {
var res = [];
for (int i=0; i<s.length; i++) {
if (s[i] != ']' && s[i] != ')' && s[i] != '}') {
res.add(s[i]);
continue;
} else {
if (res.isEmpty) {
return false;
}
}

if (s[i] == ']') {
if (res.last == '[') {
res.removeLast();
} else {
return false;
}
} else if (s[i] == ')') {
if (res.last == '(') {
res.removeLast();
} else {
return false;
}
} else if (s[i] == '}') {
if (res.last == '{') {
res.removeLast();
} else {
return false;
}
}
}
return res.length == 0;
}
}

0 comments on commit 1609f47

Please sign in to comment.