2016-03-29

posted Mar 29, 2016, 9:27 AM by Samuel Konstantinovich   [ updated Mar 29, 2016, 9:27 AM ]
in your 09 directory you should have
MyStack.java
MyQueue.java
MyLinkedList.java
and now:
ParenDemo.java

Write a function isMatching
public static boolean isMatching(String s){ ... }

It returns true only when s contains all matched sets of parenthesis of the four types: { }    [ ]   < >   ( )

All other characters are ignored, just look at the parens.
"[ ( { } ) ]" is true
"[FISHIES]" is true
"[FISH(I)ES]" is true
"[FISHIES][][][]"  is true

-You must close the most recently open parenthesis first
"({ )}" is false
-all open parens must have valid matching closing parens 
"[({" is false
-if you try to close one, there must have been an open one
")[ ( { } ) ]" is false
"({ }) (]" is false 
"[({})}" is false

Include the following main:

    public static void main(String[]args){
String input = "()()(([[]]))";
if(args.length > 0){
   input = args[0];
   System.out.println( isMatching(input));
}else{
   System.out.println("Usage:");
   System.out.println("java ParenDemo \"text\""); 
}
    }
Comments