Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I am trying to figure out the solution for finding the count of external parenthesis.
You will have more clarity on what i mean by external parenthesis in the below examples. Assume that the given expression will always be valid.

(a+b)                   //External parenthesis count =  1
(a+b)*(a-b)             //External parenthesis count =  0 
((a+b)*(a-b))           //External parenthesis count =  1
(((a+b)*(a-b)))         //External parenthesis count =  2
((a))                   //External parenthesis count =  2
(a + (b*c))             //External parenthesis count =  1
(a+b)+c                 //External parenthesis count =  0

I have tried this and somehow not able to get the right logic.
So i am reaching out for ideas. I am looking more from a logic perspective.
Posted
Updated 31-Jul-15 23:54pm
v5
Comments
Sergey Alexandrovich Kryukov 31-Jul-15 9:44am    
Define "external" precisely. Are then the parenthesis which can be removed symmetrically, the way the expression will remain the same? Do you also have to check up the balance? Why counting them? Anyway, the problem looks relatively trivial. What have you tried so far?
—SA

SQL
(a+b)                   //External parenthesis count =  1
(a+b)*(a-b)             //External parenthesis count =  0 
((a+b)*(a-b))           //External parenthesis count =  1
(((a+b)*(a-b)))         //External parenthesis count =  2
((a))                   //External parenthesis count =  2
(a + (b*c))             //External parenthesis count =  1
(a+b)+c                 //External parenthesis count =  0


For each ( ) pair delete ones (including the parenthesis) that don't contain any parenthesis themselves, eg (a+b) doesn't but (a + (b*c)) does.

SQL
                   //External parenthesis count =  1
*             //External parenthesis count =  0 
(*)           //External parenthesis count =  1
((*))         //External parenthesis count =  2
()                   //External parenthesis count =  2
(a + )             //External parenthesis count =  1
+c                 //External parenthesis count =  0


Now just count the parenthesis pairs left. Note this doesn't work for (a+b) or ((a)) so you might need a special case for when parenthesis only exists at the beginning and end.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 31-Jul-15 10:00am    
5ed.
—SA
externalCount = 0
loop
  if first or last chars are not parens
    exit loop
  Remove the external pair of parentheses.
  If the result is not well-formed
    exit loop
  externalCount++
  repeat loop
end loop

To determine if it is well-formed:
counter = 0
scan across the expression:
  if '(' then counter++
  if ')' then counter--
well formed if at end of the scan the counter is 0 and never went negative.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900