当前位置:网站首页>Stack Title: parsing Boolean expressions
Stack Title: parsing Boolean expressions
2022-07-01 05:54:00 【The great Cherny】
List of articles
subject
Title and provenance
title : Parse Boolean expressions
Source :1106. Parse Boolean expressions
difficulty
6 level
Title Description
requirement
Give you a Boolean expression in the form of a string expression \texttt{expression} expression, Returns the result of the expression .
Valid expressions follow the following conventions :
- "t" \texttt{"t"} "t", The result of operation is True \texttt{True} True;
- "f" \texttt{"f"} "f", The result of operation is False \texttt{False} False;
- "!(expr)" \texttt{"!(expr)"} "!(expr)", The operation is on the internal expression expr \texttt{expr} expr Perform logical non operation ;
- "&(expr1,expr2, … )" \texttt{"\&(expr1,expr2,\ldots)"} "&(expr1,expr2,…)", The operation process is to 2 \texttt{2} 2 One or more inner expressions expr1, expr2, … \texttt{expr1, expr2, \ldots} expr1, expr2, … Perform logical and operations ;
- "|(expr1,expr2, … )" \texttt{"|(expr1,expr2,\ldots)"} "|(expr1,expr2,…)", The operation process is to 2 \texttt{2} 2 One or more inner expressions expr1, expr2, … \texttt{expr1, expr2, \ldots} expr1, expr2, … Perform the operation of logical or .
Example
Example 1:
Input : expression = "!(f)" \texttt{expression = "!(f)"} expression = "!(f)"
Output : true \texttt{true} true
Example 2:
Input : expression = "|(f,t)" \texttt{expression = "|(f,t)"} expression = "|(f,t)"
Output : true \texttt{true} true
Example 3:
Input : expression = "&(t,f)" \texttt{expression = "\&(t,f)"} expression = "&(t,f)"
Output : false \texttt{false} false
Example 4:
Input : expression = "|(&(t,f,t),!(t))" \texttt{expression = "|(\&(t,f,t),!(t))"} expression = "|(&(t,f,t),!(t))"
Output : false \texttt{false} false
Data range
- 1 ≤ expression.length ≤ 20000 \texttt{1} \le \texttt{expression.length} \le \texttt{20000} 1≤expression.length≤20000
- expression \texttt{expression} expression from {‘(’, ‘)’, ‘&’, ‘|’, ‘!’, ‘t’, ‘f’, ‘,’} \texttt{\{`(', `)', `\&', `|', `!', `t', `f', `,'\}} {‘(’, ‘)’, ‘&’, ‘|’, ‘!’, ‘t’, ‘f’, ‘,’} The characters in consist of
- expression \texttt{expression} expression Is a valid expression given in the above form , Represents a Boolean value
solution
Ideas and algorithms
In Boolean expressions , Each logical operator ( And 、 or 、 Not ) All operations are performed on the inner expressions in the following pair of parentheses . Because we need to parse and operate according to each pair of matching parentheses , Finding matching parentheses requires the aid of stack data structures , Therefore, we can use the stack to parse Boolean expressions .
Because the comma in Boolean expression is used to separate internal expression , It does not affect the result of Boolean expression , So you can skip commas , Parsing and operation only for non bracketed characters .
Traverse Boolean expressions from left to right expression \textit{expression} expression, Characters that are not parentheses and not right parentheses are put on the stack , When you encounter the right parenthesis, you start parsing , The parsing process is as follows .
Stack the elements in the stack in turn , Until the left parenthesis , Calculate the stack ‘t’ \text{`t'} ‘t’ and ‘f’ \text{`f'} ‘f’ The number of .
Put the left bracket out of the stack , Then stack the logical operators .
Calculate the value of the current internal expression according to the logical operator :
If the logical operator is ‘&’ \text{`\&'} ‘&’, It is logic and operation , When ‘f’ \text{`f'} ‘f’ The number of is equal to 0 0 0 when , The value of the inner expression is ‘t’ \text{`t'} ‘t’, Otherwise, the value of the inner expression is ‘f’ \text{`f'} ‘f’.
If the logical operator is ‘|’ \text{`|'} ‘|’, Is a logical or operation , When ‘t’ \text{`t'} ‘t’ The number of is greater than 0 0 0 when , The value of the inner expression is ‘t’ \text{`t'} ‘t’, Otherwise, the value of the inner expression is ‘f’ \text{`f'} ‘f’.
If the logical operator is ‘!’ \text{`!'} ‘!’, Is a logical non operation , When ‘f’ \text{`f'} ‘f’ The number of is greater than 0 0 0 when , The value of the inner expression is ‘t’ \text{`t'} ‘t’, Otherwise, the value of the inner expression is ‘f’ \text{`f'} ‘f’.
Put the value of the internal expression on the stack .
Repeat the above operation , Until the Boolean expression is traversed .
because expression \textit{expression} expression Is a valid Boolean expression , Therefore, it must be able to correctly interpret , At the end of traversal , There is only one element in the stack , Is the result of a Boolean expression . If the element in the stack is ‘t’ \text{`t'} ‘t’, return true \text{true} true, Otherwise return to false \text{false} false.
Code
class Solution {
public boolean parseBoolExpr(String expression) {
Deque<Character> stack = new ArrayDeque<Character>();
int length = expression.length();
for (int i = 0; i < length; i++) {
char c = expression.charAt(i);
if (c == ',') {
continue;
} else if (c == ')') {
int tCount = 0, fCount = 0;
while (stack.peek() != '(') {
char prev = stack.pop();
if (prev == 't') {
tCount++;
} else if (prev == 'f') {
fCount++;
}
}
stack.pop();
char op = stack.pop();
if (op == '&') {
char curr = fCount == 0 ? 't' : 'f';
stack.push(curr);
} else if (op == '|') {
char curr = tCount > 0 ? 't' : 'f';
stack.push(curr);
} else if (op == '!') {
char curr = fCount > 0 ? 't' : 'f';
stack.push(curr);
}
} else {
stack.push(c);
}
}
return stack.pop() == 't';
}
}
Complexity analysis
Time complexity : O ( n ) O(n) O(n), among n n n Is a Boolean expression expression \textit{expression} expression The length of . You need to traverse the Boolean expression once , Each character can be put on and out of the stack at most once .
Spatial complexity : O ( n ) O(n) O(n), among n n n Is a Boolean expression expression \textit{expression} expression The length of . The space complexity mainly depends on the stack space , The number of elements in the stack will not exceed n n n.
边栏推荐
- Bat operation FTP upload and download command
- 表格中el-tooltip 实现换行展示
- excel动态图表
- Advanced cross platform application development (III): online resource upgrade / hot update with uni app
- 【考研高数 武忠祥+880版 自用】高数第二章基础阶段思维导图
- 亲爱的派盘用户,我要向您表白!
- How to transmit and share 4GB large files remotely in real time?
- OpenGL ES: (3) EGL、EGL绘图的基本步骤、EGLSurface、ANativeWindow
- SystemVerilog学习-09-进程间同步、通信和虚方法
- Primary application case of Excel DuPont analyzer
猜你喜欢
随机推荐
Vscode function annotation / file header annotation shortcut
Servlet
Oracle create user + Role
Bat operation FTP upload and download command
Advanced cross platform application development (II): uni app practice
It's not that you have a bad mind, but that you haven't found the right tool
如何添加葫芦儿派盘
C语言初阶——实现扫雷游戏
2022.6.30-----leetcode. one thousand one hundred and seventy-five
机械臂速成小指南(六):步进电机驱动器
千万不要把笔记视频乱放!
为了保护自己的数据,他奋斗了一天一夜
HCM 初学 ( 一 ) - 简介
MySQL数据迁移遇到的一些错误
OpenGL ES: (4) EGL API详解 (转)
基于LabVIEW的计时器
Oracle sequence + trigger
Fragment upload and breakpoint resume
葫芦儿 APP 使用帮助
POL8901 LVDS转MIPI DSI 支持旋转图像处理芯片









