{"id":27010,"date":"2018-01-02T20:44:07","date_gmt":"2018-01-02T15:14:07","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27010"},"modified":"2018-01-02T20:44:07","modified_gmt":"2018-01-02T15:14:07","slug":"check-balanced-parentheses-expression","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/check-balanced-parentheses-expression\/","title":{"rendered":"Check for balanced parentheses in an expression"},"content":{"rendered":"<p>Given an expression string exp , write a program to examine whether the pairs and the orders of \u201c{\u201c,\u201d}\u201d,\u201d(\u201c,\u201d)\u201d,\u201d[\u201c,\u201d]\u201d are correct in exp. <span id=\"more-6547\"><\/span>For example, the program should print true for exp = \u201c[()]{}{[()()]()}\u201d and false for exp = \u201c[(])\u201d<\/p>\n<p><strong>Algorithm:<\/strong><br \/>\n1) Declare a character stack S.<br \/>\n2) Now traverse the expression string exp.<br \/>\na) If the current character is a starting bracket (\u2018(\u2018 or \u2018{\u2018 or \u2018[\u2018) then push it to stack.<br \/>\nb) If the current character is a closing bracket (\u2018)\u2019 or \u2018}\u2019 or \u2018]\u2019) then pop from stack and if the popped character is the matching starting bracket then fine else parenthesis are not balanced.<br \/>\n3) After complete traversal, if there is some starting bracket left in stack then \u201cnot balanced\u201d<\/p>\n<p>Implementation:<\/p>\n<p><strong>C Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/>#include&lt;stdlib.h&gt;<br\/>#define bool int<br\/> <br\/>\/* structure of a stack node *\/<br\/>struct sNode<br\/>{<br\/>   char data;<br\/>   struct sNode *next;<br\/>};<br\/> <br\/>\/* Function to push an item to stack*\/<br\/>void push(struct sNode** top_ref, int new_data);<br\/> <br\/>\/* Function to pop an item from stack*\/<br\/>int pop(struct sNode** top_ref);<br\/> <br\/>\/* Returns 1 if character1 and character2 are matching left<br\/>   and right Parenthesis *\/<br\/>bool isMatchingPair(char character1, char character2)<br\/>{<br\/>   if (character1 == &#039;(&#039; &amp;&amp; character2 == &#039;)&#039;)<br\/>     return 1;<br\/>   else if (character1 == &#039;{&#039; &amp;&amp; character2 == &#039;}&#039;)<br\/>     return 1;<br\/>   else if (character1 == &#039;[&#039; &amp;&amp; character2 == &#039;]&#039;)<br\/>     return 1;<br\/>   else<br\/>     return 0;<br\/>}<br\/> <br\/>\/*Return 1 if expression has balanced Parenthesis *\/<br\/>bool areParenthesisBalanced(char exp[])<br\/>{<br\/>   int i = 0;<br\/> <br\/>   \/* Declare an empty character stack *\/<br\/>   struct sNode *stack = NULL;<br\/> <br\/>   \/* Traverse the given expression to check matching parenthesis *\/<br\/>   while (exp[i])<br\/>   {<br\/>      \/*If the exp[i] is a starting parenthesis then push it*\/<br\/>      if (exp[i] == &#039;{&#039; || exp[i] == &#039;(&#039; || exp[i] == &#039;[&#039;)<br\/>        push(&amp;stack, exp[i]);<br\/> <br\/>      \/* If exp[i] is an ending parenthesis then pop from stack and <br\/>          check if the popped parenthesis is a matching pair*\/<br\/>      if (exp[i] == &#039;}&#039; || exp[i] == &#039;)&#039; || exp[i] == &#039;]&#039;)<br\/>      {<br\/>             <br\/>          \/*If we see an ending parenthesis without a pair then return false*\/<br\/>         if (stack == NULL)<br\/>           return 0; <br\/> <br\/>         \/* Pop the top element from stack, if it is not a pair <br\/>            parenthesis of character then there is a mismatch.<br\/>            This happens for expressions like {(}) *\/<br\/>         else if ( !isMatchingPair(pop(&amp;stack), exp[i]) )<br\/>           return 0;<br\/>      }<br\/>      i++;<br\/>   }<br\/>    <br\/>   \/* If there is something left in expression then there is a starting<br\/>      parenthesis without a closing parenthesis *\/<br\/>   if (stack == NULL)<br\/>     return 1; \/*balanced*\/<br\/>   else<br\/>     return 0;  \/*not balanced*\/<br\/>} <br\/> <br\/>\/* UTILITY FUNCTIONS *\/<br\/>\/*driver program to test above functions*\/<br\/>int main()<br\/>{<br\/>  char exp[100] = &quot;{()}[]&quot;;<br\/>  if (areParenthesisBalanced(exp))<br\/>    printf(&quot;\\n Balanced &quot;);<br\/>  else<br\/>    printf(&quot;\\n Not Balanced &quot;);  <br\/>  return 0;<br\/>}    <br\/> <br\/>\/* Function to push an item to stack*\/<br\/>void push(struct sNode** top_ref, int new_data)<br\/>{<br\/>  \/* allocate node *\/<br\/>  struct sNode* new_node =<br\/>            (struct sNode*) malloc(sizeof(struct sNode));<br\/> <br\/>  if (new_node == NULL)<br\/>  {<br\/>     printf(&quot;Stack overflow \\n&quot;);<br\/>     getchar();<br\/>     exit(0);<br\/>  }           <br\/> <br\/>  \/* put in the data  *\/<br\/>  new_node-&gt;data  = new_data;<br\/> <br\/>  \/* link the old list off the new node *\/<br\/>  new_node-&gt;next = (*top_ref);  <br\/> <br\/>  \/* move the head to point to the new node *\/<br\/>  (*top_ref)    = new_node;<br\/>}<br\/> <br\/>\/* Function to pop an item from stack*\/<br\/>int pop(struct sNode** top_ref)<br\/>{<br\/>  char res;<br\/>  struct sNode *top;<br\/> <br\/>  \/*If stack is empty then error *\/<br\/>  if (*top_ref == NULL)<br\/>  {<br\/>     printf(&quot;Stack overflow \\n&quot;);<br\/>     getchar();<br\/>     exit(0);<br\/>  }<br\/>  else<br\/>  {<br\/>     top = *top_ref;<br\/>     res = top-&gt;data;<br\/>     *top_ref = top-&gt;next;<br\/>     free(top);<br\/>     return res;<br\/>  }<br\/>}<\/code><\/pre> <\/div>\n<p>Time Complexity: O(n)<br \/>\nAuxiliary Space: O(n) for stack.<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Check for balanced parentheses in an expression- Data Structure &#8211; Given an expression string exp , write a program to examine whether the pairs <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73012,79607],"tags":[80614,80613,80615,80610,80616,80606,80608,80609,80611,80612,80607],"class_list":["post-27010","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-stack","tag-balanced-parentheses-algorithm","tag-balanced-parentheses-geeksforgeeks","tag-balanced-parentheses-java","tag-balanced-parentheses-python","tag-balanced-parentheses-recursion","tag-balanced-parentheses-using-stack-in-java","tag-balanced-parentheses-without-stack","tag-check-for-balanced-parentheses-in-an-expression-in-python","tag-parenthesis-matching-algorithm","tag-parenthesis-matching-using-stack-in-c","tag-parenthesis-matching-using-stack-in-data-structure"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27010"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27010\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}