Raskite, ar išraiška turi pasikartojančius skliaustus, ar ne

Atsižvelgiant į subalansuotą išraišką, suraskite, ar joje yra pasikartojančių skliaustų, ar ne. Skliaustų rinkinys pasikartoja, jei ta pati poraiška yra apsupta kelių skliaustų. 

Pavyzdžiai:  

    Below expressions have duplicate parenthesis -      
((a+b)+((c+d)))
The subexpression 'c+d' is surrounded by two
pairs of brackets.

(((a+(b)))+(c+d))
The subexpression 'a+(b)' is surrounded by two
pairs of brackets.

(((a+(b))+c+d))
The whole expression is surrounded by two
pairs of brackets.

((a+(b))+(c+d))
(b) and ((a+(b)) is surrounded by two
pairs of brackets but it will not be counted as duplicate.

Below expressions don't have any duplicate parenthesis -
((a+b)+(c+d))
No subexpression is surrounded by duplicate
brackets.

Galima daryti prielaidą, kad pateikta išraiška galioja ir jame nėra tarpų. 

Idėja yra naudoti krūvą. Pakartokite pateiktą išraišką ir kiekvieną išraiškos simbolį, jei simbolis yra atviras skliaustelis „(“ arba bet kuris iš operatorių ar operandų, stumkite jį į krūvos viršų. Jei simbolis yra uždaras skliaustelis „)“, tada iškelkite simbolius iš krūvos iki atitikimo atviram skliausčiui „(“ ir naudojamas skaitiklis, kurio reikšmė padidinama, kol randamas kiekvieno kaktinio simbolio reikšmė. simboliai, sutinkami tarp atidarymo ir uždarymo skliaustų pora, kuri lygi skaitiklio reikšmei, yra mažesnė nei 1, tada randama pasikartojančių skliaustų pora, kitu atveju perteklinių skliaustų porų nėra. Pavyzdžiui, (((a+b))+c) aplink „a+b“ yra pasikartojantys skliaustai. Kai aptinkamas antrasis ')' po a+b, krūvoje yra '(('. Kadangi krūvos viršus yra pradinis skliaustas, galima daryti išvadą, kad yra pasikartojančių skliausteliuose.

Žemiau yra aukščiau pateiktos idėjos įgyvendinimas: 

C++
   // C++ program to find duplicate parenthesis in a   // balanced expression   #include          using     namespace     std  ;   // Function to find duplicate parenthesis in a   // balanced expression   bool     findDuplicateparenthesis  (  string     str  )   {      // create a stack of characters      stack   <  char  >     Stack  ;      // Iterate through the given expression      for     (  char     ch     :     str  )      {      // if current character is close parenthesis ')'      if     (  ch     ==     ')'  )      {      // pop character from the stack      char     top     =     Stack  .  top  ();      Stack  .  pop  ();      // stores the number of characters between a       // closing and opening parenthesis      // if this count is less than or equal to 1      // then the brackets are redundant else not      int     elementsInside     =     0  ;      while     (  top     !=     '('  )      {      elementsInside  ++  ;      top     =     Stack  .  top  ();      Stack  .  pop  ();      }      if  (  elementsInside      <     1  )     {      return     1  ;      }      }      // push open parenthesis '(' operators and      // operands to stack      else      Stack  .  push  (  ch  );      }      // No duplicates found      return     false  ;   }   // Driver code   int     main  ()   {      // input balanced expression      string     str     =     '(((a+(b))+(c+d)))'  ;      if     (  findDuplicateparenthesis  (  str  ))      cout      < <     'Duplicate Found '  ;      else      cout      < <     'No Duplicates Found '  ;      return     0  ;   }   
Java
   import     java.util.Stack  ;   // Java program to find duplicate parenthesis in a    // balanced expression    public     class   GFG     {   // Function to find duplicate parenthesis in a    // balanced expression       static     boolean     findDuplicateparenthesis  (  String     s  )     {      // create a stack of characters       Stack   <  Character  >     Stack     =     new     Stack   <>  ();      // Iterate through the given expression       char  []     str     =     s  .  toCharArray  ();      for     (  char     ch     :     str  )     {      // if current character is close parenthesis ')'       if     (  ch     ==     ')'  )     {      // pop character from the stack       char     top     =     Stack  .  peek  ();      Stack  .  pop  ();      // stores the number of characters between a       // closing and opening parenthesis       // if this count is less than or equal to 1       // then the brackets are redundant else not       int     elementsInside     =     0  ;      while     (  top     !=     '('  )     {      elementsInside  ++  ;      top     =     Stack  .  peek  ();      Stack  .  pop  ();      }      if     (  elementsInside      <     1  )     {      return     true  ;      }      }     // push open parenthesis '(' operators and       // operands to stack       else     {      Stack  .  push  (  ch  );      }      }      // No duplicates found       return     false  ;      }   // Driver code    public     static     void     main  (  String  []     args  )     {      // input balanced expression       String     str     =     '(((a+(b))+(c+d)))'  ;      if     (  findDuplicateparenthesis  (  str  ))     {      System  .  out  .  println  (  'Duplicate Found '  );      }     else     {      System  .  out  .  println  (  'No Duplicates Found '  );      }      }   }   
Python
   # Python3 program to find duplicate    # parenthesis in a balanced expression    # Function to find duplicate parenthesis    # in a balanced expression    def   findDuplicateparenthesis  (  string  ):   # create a stack of characters    Stack   =   []   # Iterate through the given expression    for   ch   in   string  :   # if current character is    # close parenthesis ')'    if   ch   ==   ')'  :   # pop character from the stack    top   =   Stack  .  pop  ()   # stores the number of characters between    # a closing and opening parenthesis    # if this count is less than or equal to 1    # then the brackets are redundant else not    elementsInside   =   0   while   top   !=   '('  :   elementsInside   +=   1   top   =   Stack  .  pop  ()   if   elementsInside    <   1  :   return   True   # push open parenthesis '(' operators    # and operands to stack    else  :   Stack  .  append  (  ch  )   # No duplicates found    return   False   # Driver Code   if   __name__   ==   '__main__'  :   # input balanced expression    string   =   '(((a+(b))+(c+d)))'   if   findDuplicateparenthesis  (  string  )   ==   True  :   print  (  'Duplicate Found'  )   else  :   print  (  'No Duplicates Found'  )   # This code is contributed by Rituraj Jain   
C#
   // C# program to find duplicate parenthesis    // in a balanced expression    using     System  ;   using     System.Collections.Generic  ;   class     GFG      {   // Function to find duplicate parenthesis    // in a balanced expression    static     Boolean     findDuplicateparenthesis  (  String     s  )      {      // create a stack of characters       Stack   <  char  >     Stack     =     new     Stack   <  char  >  ();      // Iterate through the given expression       char  []     str     =     s  .  ToCharArray  ();      foreach     (  char     ch     in     str  )         {      // if current character is       // close parenthesis ')'       if     (  ch     ==     ')'  )         {      // pop character from the stack       char     top     =     Stack  .  Peek  ();      Stack  .  Pop  ();      // stores the number of characters between      // a closing and opening parenthesis       // if this count is less than or equal to 1       // then the brackets are redundant else not       int     elementsInside     =     0  ;      while     (  top     !=     '('  )         {      elementsInside  ++  ;      top     =     Stack  .  Peek  ();      Stack  .  Pop  ();      }      if     (  elementsInside      <     1  )         {      return     true  ;      }      }             // push open parenthesis '('       // operators and operands to stack       else         {      Stack  .  Push  (  ch  );      }      }      // No duplicates found       return     false  ;   }   // Driver code    public     static     void     Main  (  String  []     args  )   {      // input balanced expression       String     str     =     '(((a+(b))+(c+d)))'  ;      if     (  findDuplicateparenthesis  (  str  ))      {      Console  .  WriteLine  (  'Duplicate Found '  );      }         else         {      Console  .  WriteLine  (  'No Duplicates Found '  );      }   }   }   // This code is contributed by 29AjayKumar   
JavaScript
   // JavaScript program to find duplicate parentheses in a balanced expression   function     findDuplicateParenthesis  (  s  )     {      let     stack     =     [];      // Iterate through the given expression      for     (  let     ch     of     s  )     {          // If current character is a closing parenthesis ')'      if     (  ch     ===     ')'  )     {      let     top     =     stack  .  pop  ();          // Count the number of elements      // inside the parentheses      let     elementsInside     =     0  ;      while     (  top     !==     '('  )     {      elementsInside  ++  ;      top     =     stack  .  pop  ();      }          // If there's nothing or only one element       // inside it's redundant      if     (  elementsInside      <     1  )     {      return     true  ;      }      }         // Push open parenthesis '(' operators and operands to stack      else     {      stack  .  push  (  ch  );      }      }      // No duplicates found      return     false  ;   }   // Driver code   let     str     =     '(((a+(b))+(c+d)))'  ;   if     (  findDuplicateParenthesis  (  str  ))     {      console  .  log  (  'Duplicate Found'  );   }     else     {      console  .  log  (  'No Duplicates Found'  );   }   // This code is contributed by rag2127   

Išvestis
Duplicate Found  

Išvestis:  

 Duplicate Found  

Laiko sudėtingumas aukščiau pateikto tirpalo yra O(n). 

Pagalbinė erdvė programos naudojamas yra O(n).