SHARE
TWEET

Untitled

a guest Jan 24th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.Serializable;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Stack;
  5.  
  6.  
  7. class MyCode{
  8.    
  9.     public static void main(String[] args) {
  10.        
  11.         System.out.println("(a[0]+b[2c[6]]) {24 + 53}  passed:" + parensMatch("(a[0]+b[2c[6]]) {24 + 53}"));
  12.         System.out.println("f(e(d))                    passed:" + parensMatch("f(e(d))"));
  13.         System.out.println("[()]{}([])                 passed:" + parensMatch("[()]{}([])"));
  14.         System.out.println("((b)                       passed:" + !parensMatch("((b)"));
  15.         System.out.println("(c]                        passed:" + !parensMatch("(c]"));
  16.         System.out.println("{(a[])                     passed:" + !parensMatch("{(a[])"));
  17.         System.out.println("([)]                       passed:" + !parensMatch("([)]"));
  18.         System.out.println(")(                         passed:" + !parensMatch(")("));
  19.         System.out.println(" (empty)                   passed:" + parensMatch(""));
  20.        
  21.     }
  22.    
  23.     public static boolean parensMatch(String sentence){
  24.     List<Character> expected = new ArrayList<Character>();
  25.         for (Character letter : sentence.toCharArray()){
  26.            
  27.             if (expected.contains(letter)){
  28.                 if (expected.get(expected.lastIndexOf(letter)).equals(expected.get(expected.size()-1)))
  29.                         expected.remove(expected.lastIndexOf(letter));
  30.                 else break;
  31.             }
  32.            
  33.             if (letter=='(') expected.add(')');
  34.             else if (letter=='[') expected.add(']');
  35.             else if (letter=='{') expected.add('}');
  36.         }
  37.         return expected.isEmpty();
  38.     }
  39.    
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top