lamiastella

valid

Mar 12th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.06 KB | None | 0 0
  1. import java.util.Stack;
  2.  
  3. /**
  4.  * Created by mona on 3/12/16.
  5.  */
  6. public class ValidParanthesis {
  7.     public static boolean isValid(String s){
  8.  
  9.         if (s.length()==1 || s==null){
  10.             return true;
  11.         }
  12.         else {
  13.             int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0;
  14.             for (int i = 0; i < s.length(); i++) {
  15.                 if (s.charAt(i) == '{') {
  16.                     cnt1++;
  17.                 } else if (s.charAt(i) == '}') {
  18.                     cnt2++;
  19.                 } else if (s.charAt(i) == '(') {
  20.                     cnt3++;
  21.                 } else if (s.charAt(i) == ')') {
  22.                     cnt4++;
  23.                 } else if (s.charAt(i) == '[') {
  24.                     cnt5++;
  25.                 } else if (s.charAt(i) == ']') {
  26.                     cnt6++;
  27.                 }
  28.             }
  29.             return ((cnt1 == cnt2) && (cnt3 == cnt4) && (cnt5 == cnt6));
  30.         }
  31.     }
  32.     public static void main(String[] args){
  33.         String s="]";
  34.         System.out.println(isValid(s));
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment