package com.fishercoder.solutions;

import java.util.Stack;

/**
 * 32. Longest Valid Parentheses
 *
 * Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
 *
 * Example 1:
 * Input: "(()"
 * Output: 2
 * Explanation: The longest valid parentheses substring is "()"
 *
 * Example 2:
 * Input: ")()())"
 * Output: 4
 * Explanation: The longest valid parentheses substring is "()()"
 */
public class _32 {
  public static class Solution1 {
    public int longestValidParentheses(String s) {
      int result = 0;
      Stack<Integer> stack = new Stack();
      stack.push(-1);
      for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == '(') {
          stack.push(i);
        } else {
          stack.pop();
          if (stack.isEmpty()) {
            stack.push(i);
          } else {
            result = Math.max(result, i - stack.peek());
          }
        }
      }
      return result;
    }
  }
}

@@ -58,413 +58,8 @@
 k;%0A%0A
-/**%0A * 32. Longest Valid Parentheses%0A *%0A * Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.%0A *%0A * Example 1:%0A * Input: %22(()%22%0A * Output: 2%0A * Explanation: The longest valid parentheses substring is %22()%22%0A *%0A * Example 2:%0A * Input: %22)()())%22%0A * Output: 4%0A * Explanation: The longest valid parentheses substring is %22()()%22%0A */%0A
 publ
@@ -75,16 +75,18 @@
 _32 %7B%0A  
+ 
+ 
 public s
@@ -109,16 +109,20 @@
 tion1 %7B%0A
+    
     publ
@@ -170,16 +170,22 @@
 %7B%0A      
+   
+   
 int resu
@@ -192,16 +192,22 @@
 lt = 0;%0A
+      
       St
@@ -236,24 +236,30 @@
 ew Stack();%0A
+      
       stack.
@@ -274,16 +274,22 @@
 ;%0A      
+   
+   
 for (int
@@ -315,24 +315,32 @@
 h(); i++) %7B%0A
+        
         if (
@@ -363,32 +363,42 @@
 (') %7B%0A          
+      
+    
 stack.push(i);%0A 
@@ -396,32 +396,40 @@
 ush(i);%0A        
+     
+   
 %7D else %7B%0A       
@@ -427,24 +427,34 @@
 %7B%0A          
+     
+     
 stack.pop();
@@ -450,24 +450,34 @@
 tack.pop();%0A
+          
           if
@@ -501,32 +501,44 @@
 ) %7B%0A            
+  
+          
 stack.push(i);%0A 
@@ -538,32 +538,42 @@
 h(i);%0A          
+       
+   
 %7D else %7B%0A       
@@ -557,32 +557,44 @@
        %7D else %7B%0A
+            
             resu
@@ -636,32 +636,42 @@
 k());%0A          
+  
+        
 %7D%0A        %7D%0A    
@@ -664,24 +664,32 @@
         
+ 
+       
 %7D%0A      
 %7D%0A      
@@ -676,26 +676,38 @@
     %7D%0A      
+ 
-%7D%0A
+     %7D%0A      
       return
@@ -719,16 +719,22 @@
 lt;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 32