Dart Coding Essentials: 101

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;
}

--

--

Aman Khan Roohani

Cross Platform Developer (Flutter) | Google Groups Contributor