Dart Coding Essentials: 101
Oct 27, 2023
Given a string containing just the characters '('
and ')'
, return the length of the longest valid (well-formed) parentheses
Example 1:
Input: s = "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()".
Example 2:
Input: s = ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()".
Example 3:
Input: s = ""
Output: 0
void main() {
List<String> temp = longestValidParentheses("(()");
print("Output ${temp.length}");
print("The longest valid parentheses substring is ${temp.first} and ${temp.last}");
}
List<String> longestValidParentheses(String s) {
List<String> temp = List.empty(growable:true);
for(int i=0;i< (s.length-1);i++){
if(s[i] == "("){
if(s[i+1] == ")"){
print(s[i]);
print(s[i+1]);
temp.add(s[i]);
temp.add(s[i+1]);
}
}
}
return temp;
}