• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stacks and Parenthesis  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to write a program involving a stack of two different kinds of parenthesis for a method that returns true or false depending on certain conditions. What I have written so far is a method that returns true if a string has all matching parenthesis, false otherwise. I want to modify this however so that if the string has any curly brackets '{' '}' containing any parenthesis () inside of the curcly brackets it returns false. For example "({})" would return true but "{()}" would return false.

"({{}})" == true
"(({)})" or "{}" == false

I would like to have this working for some discrete mathematics problems I am working on so would anyone know how I can modify the below code?

Thanks.





 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachell Zammit wrote:I want to modify this however so that if the string has any curly brackets '{' '}' containing any parenthesis () inside of the curcly brackets it returns false. For example "({})" would return true but "{()}" would return false.

"({{}})" == true
"(({)})" or "{}" == false

That last condition ("{}") is probably the trickiest of all, because it doesn't conform to the same pattern as the others - and I'm presuming that there can be other arbitrary characters between the brackets, so in fact, the pattern is: "...{...}...".

My advice: tackle all the other conditions first, because they're all similar and you've almost got the code for them written.

There are several ways of tackling it, so I'm just going to offer a few suggestions:
  • What about keeping a separate Stack for each set of brackets?
  • What about storing indexes in your Stack(s) instead of characters?
  • What about using Stack.size()? This might be particularly useful for that last condition.

  • It's very unfortunate, but Stack is basically a legacy class, and its docs actually suggest that you use Deque (java.util.Deque) instead (you shouldn't have to change anything except the type because Deque contains all the same methods). For one thing, Stack is synchronized; and I suspect you don't need that.

    Winston
     
    Marshal
    Posts: 56600
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think there is a bit of hardware obstructing your progress. It is long and thin and black and connects your computer to the wall. Remove it.

    Now (if you can still read this bit ‍) get a piece of paper and a pencil and an eraser. That third item of equipment is by far the most important.
    Write down how you are going to do it, and get it into words of one syllable. [“Parenthesis” may be difficult to get down to one syllable.] Now you can reconnect the bit of computer hardware and start writing code.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!