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

13 Sep, 2021
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: 2Explanation: Realizing the expression requires 1 AND gate represented by ‘.’ and 1 OR gate represented by ‘+’. 
Input: S = “(1 – A). B+C”Output: 3Explanation: 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:
Iterate over the characters of the string.Initialize, count of gates to 0.If the current character is either ‘.’ or ‘+’, or ‘1’, then increment the count of gates by 1Print the count of gates required.
Iterate over the characters of the string.
Initialize, count of gates to 0.
If the current character is either ‘.’ or ‘+’, or ‘1’, then increment the count of gates by 1
Print the count of gates required.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation of// the above approach#include <bits/stdc++.h>using namespace std; // Function to count the total// number of gates required to// realize the boolean expression Svoid numberOfGates(string s){    // Length of the string    int N = s.size();     // Stores the count    // of total gates    int ans = 0;     // Traverse the string    for (int i = 0; i < (int)s.size(); i++) {         // AND, OR and NOT Gate        if (s[i] == '.' || s[i] == '+'            || s[i] == '1') {            ans++;        }    }     // Print the count    // of gates required    cout << ans;} // Driver Codeint main(){    // Input    string S = "(1-A).B+C";     // Function call to count the    // total number of gates required    numberOfGates(S);}
// Java implementation of// the above approachclass GFG{ // Function to count the total// number of gates required to// realize the boolean expression Sstatic void numberOfGates(String s){         // Length of the string    int N = s.length();     // Stores the count    // of total gates    int ans = 0;     // Traverse the string    for(int i = 0; i < (int)s.length(); i++)    {                 // AND, OR and NOT Gate        if (s.charAt(i) == '.' ||            s.charAt(i) == '+' ||            s.charAt(i) == '1')        {            ans++;        }    }     // Print the count    // of gates required    System.out.println(ans);} // Driver Codepublic static void main(String[] args){         // Input    String S = "(1-A).B+C";     // Function call to count the    // total number of gates required    numberOfGates(S);}} // This code is contributed by user_qa7r
# Python3 implementation of# the above approach # Function to count the total# number of gates required to# realize the boolean expression Sdef numberOfGates(s):     # Length of the string    N = len(s)     # Stores the count    # of total gates    ans = 0     # Traverse the string    for i in range(len(s)):         # AND, OR and NOT Gate        if (s[i] == '.' or s[i] == '+' or            s[i] == '1'):            ans += 1     # Print the count    # of gates required    print(ans, end = "") # Driver Codeif __name__ == "__main__":     # Input    S = "(1-A).B+C"     # Function call to count the    # total number of gates required    numberOfGates(S) # This code is contributed by AnkThon
// C# implementation of// the above approachusing System;public class GFG{ // Function to count the total// number of gates required to// realize the boolean expression Sstatic void numberOfGates(string s){         // Length of the string    int N = s.Length;     // Stores the count    // of total gates    int ans = 0;     // Traverse the string    for(int i = 0; i < s.Length; i++)    {                 // AND, OR and NOT Gate        if (s[i] == '.' ||            s[i] == '+' ||            s[i] == '1')        {            ans++;        }    }     // Print the count    // of gates required    Console.WriteLine(ans);} // Driver Codepublic static void Main(string[] args){         // Input    string S = "(1-A).B+C";     // Function call to count the    // total number of gates required    numberOfGates(S);}} // This code is contributed by AnkThon
<script>// JavaScript program for the above approach // Function to count the total// number of gates required to// realize the boolean expression Sfunction numberOfGates(s){          // Length of the string    let N = s.length;      // Stores the count    // of total gates    let ans = 0;      // Traverse the string    for(let i = 0; i < s.length; i++)    {                  // AND, OR and NOT Gate        if (s[i] == '.' ||            s[i] == '+' ||            s[i] == '1')        {            ans++;        }    }      // Print the count    // of gates required    document.write(ans);} // Driver Code     // Input    let S = "(1-A).B+C";      // Function call to count the    // total number of gates required    numberOfGates(S);     </script>
3
 
Time Complexity: O(N)Auxiliary Space: O(1)
 
hritikrommie
ankthon
chinmoy1997pal
arorakashish0911
logical-thinking
Technical Scripter 2020
Mathematical
Searching
Strings
Technical Scripter
Searching
Strings
Mathematical
logical-thinking
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Linear Search
K'th Smallest/Largest Element in Unsorted Array | Set 1
Search an element in a sorted and rotated array