tuff question in Java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2008
Posts: 6
Reputation: Adami is an unknown quantity at this point 
Solved Threads: 0
Adami Adami is offline Offline
Newbie Poster

tuff question in Java

 
0
  #1
Jan 16th, 2008
I need to create a static boolean method, boolean isValid(String str) which gets a string and check the brackets in it...

a correct expression is an expression as one of the following:

1) an empty string or a string that includes space only in it.

2) st1 +"" +st2, which st1 and st2 are correct bracket expressions.

3) ("+st+") or ["+st+"] or {"+st+"} which st is a correct bracket expression.

examples:

isValid(" ") == true

isValid("() [] ") == true

isValid("{] [}") == false

Another condition is that the complexity must be linear which says O(0).

Is anyone please can write me a try of that method ?

Thanks !

Adam.
Last edited by Adami; Jan 16th, 2008 at 10:46 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,689
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: tuff question in Java

 
0
  #2
Jan 16th, 2008
Try using str.charAt(i) in a for-loop. This method returns one by one each char of the String. Then you can count the different brackets that open and close and make sure that each time the bracket count must be positive or zero. At the end of the loop all bracket counts must be zero. You must also check that you don't have a case such as this which is wrong :
{ [ } ]

  1. public static boolean isValid(String str) {
  2. //code here
  3. }
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: Adami is an unknown quantity at this point 
Solved Threads: 0
Adami Adami is offline Offline
Newbie Poster

Re: tuff question in Java

 
0
  #3
Jan 16th, 2008
Originally Posted by javaAddict View Post
Try using str.charAt(i) in a for-loop. This method returns one by one each char of the String. Then you can count the different brackets that open and close and make sure that each time the bracket count must be positive or zero. At the end of the loop all bracket counts must be zero. You must also check that you don't have a case such as this which is wrong :
{ [ } ]

  1. public static boolean isValid(String str) {
  2. //code here
  3. }
The idea with the str.charAt(i) looks not bad ! but.... that algorithm does not speaks about the { [ } ] problem ... I've tried already some solutions but zero results... can you show me a suggestion for a real java code for it ? (linear one...)

thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,492
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 519
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: tuff question in Java

 
0
  #4
Jan 16th, 2008
Obviously the point of your homework is for YOU to come up with a solution. Handing you the solution defeats the purpose. Post your code and describe what troubles you are having if you would like assistance.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 765
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: tuff question in Java

 
0
  #5
Jan 16th, 2008
Let's see what you've tried. Easiest method would be to just use regular expressions.

The method javaAddict describes is very simple. When you encounter an opening bracket, increment your bracket counter. When you encounter a closing bracket, decrement the counter. If the counter is 0 at the end of the FOR loop, then the equation should be valid.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,492
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 519
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: tuff question in Java

 
0
  #6
Jan 16th, 2008
Stacks are useful for such things as well.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,689
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is online now Online
Posting Virtuoso

Re: tuff question in Java

 
0
  #7
Jan 17th, 2008
In case you have something like this: { [ } ], perhaps you should have some boolean variables for the different brackets(isCloded, isOpened) so you can check that you cannot close something if you are inside a different unclosed bracket. Try to remember the most recent open bracket
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: Adami is an unknown quantity at this point 
Solved Threads: 0
Adami Adami is offline Offline
Newbie Poster

Re: tuff question in Java

 
0
  #8
Jan 20th, 2008
Originally Posted by javaAddict View Post
In case you have something like this: { [ } ], perhaps you should have some boolean variables for the different brackets(isCloded, isOpened) so you can check that you cannot close something if you are inside a different unclosed bracket. Try to remember the most recent open bracket
gotcha
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: tuff question in Java

 
0
  #9
Jan 21st, 2008
Or you could just use an abstract data structure, such as a stack which is ideal for this, like Ezzaral suggested in post #6
Last edited by iamthwee; Jan 21st, 2008 at 5:29 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC