ICS 160E/ECE 144 - Algorithms Homework 2, 25 Points
Due: Monday, April 14, 2003
-
5 points.
Problem R-2.1 from Goodrich-Tamassia.
-
5 points.
Problem R-2.3 from Goodrich-Tamassia.
-
5 points.
Problem C-2.1 from Goodrich-Tamassia.
-
5 points.
Problem C-2.2 from Goodrich-Tamassia.
For a hint, click here.
-
5 points.
Write a small Java function isMatched(String s), which
takes a string s consisting of (, ), {, }, [, and ]
characters, and returns true if and only if the string of parens,
braces, and brackets match.
For example isMatched("()()[]{({[]})}") is true,
whereas
isMatched("())[]{([{]})}") is false.
Also, isMatched("({)}") is false, but isMatched("({[]{}()})") is
true.
Your program should use a java.util.Stack object and run
in linear time (proportional to the length of s).
Turn in a complete listing of
your program, which must contain useful comments, and
show the output of this method on the following strings:
- ()(()){([()])}
- ((()(()){([()])}
- )(()){([()])}
- ({[])}
- (
Note: This problem must be done independently. You cannot work with
someone else on problem 5!
Hints: Look
here,
here,
and
here.