给定一个长度为N的字符串S ,表示一个布尔表达式,任务是找到实现给定表达式所需的最小 AND、OR 和 NOT 门数。
例子:
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 ‘-‘.
处理方法:按照以下步骤解决问题:
- 遍历字符串的字符。
- 初始化,门数为0 。
- 如果当前字符是‘.’或‘+’或‘1’ ,然后将门的计数增加1
- 打印所需的门数。
下面是上述方法的实现:
C++
// C++ implementataion of
// the above approach
#include
using namespace std;
// Function to coutn the total
// number of gates required to
// realize the boolean expression S
void 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 Code
int main()
{
// Input
string S = "(1-A).B+C";
// Function call to count the
// total number of gates required
numberOfGates(S);
}
Java
// Java implementataion of
// the above approach
class GFG{
// Function to coutn the total
// number of gates required to
// realize the boolean expression S
static 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 Code
public 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
# Python3 implementataion of
# the above approach
# Function to coutn the total
# number of gates required to
# realize the boolean expression S
def 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 Code
if __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#
// C# implementataion of
// the above approach
using System;
public class GFG
{
// Function to coutn the total
// number of gates required to
// realize the boolean expression S
static 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 Code
public 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
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live