VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-of-basic-logic-gates-required-to-realize-given-boolean-expression/

⇱ Minimum number of basic logic gates required to realize given Boolean expression - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum number of basic logic gates required to realize given Boolean expression

Last Updated : 23 Jul, 2025

Given a string S of length N representing a boolean expression, the task is to find the minimum number of AND, OR, and NOT gates required to realize the given expression.

Examples:

Input: S = "A+B.C"
Output: 2
Explanation: Realizing the expression requires 1 AND gate represented by '.' and 1 OR gate represented by '+'. 

Input: S = "(1 - A). B+C"
Output: 3
Explanation: Realizing the expression requires 1 AND gate represented by '.' and 1 OR gate represented by '+' and 1 NOT gate represented by '-'. 

Approach: Follow the steps below to solve the problem:

  1. Iterate over the characters of the string.
  2. Initialize, count of gates to 0.
  3. If the current character is either '.' or '+', or '1', then increment the count of gates by 1
  4. Print the count of gates required.

Below is the implementation of the above approach:


Output: 
3

 

Time Complexity: O(N)
Auxiliary Space: O(1)


 

Comment