当前位置:网站首页>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.
边栏推荐
猜你喜欢

uniapp树形层级选择器

亲爱的派盘用户,我要向您表白!

LED lighting used in health lighting

Talking from mlperf: how to lead the next wave of AI accelerator

Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers

Crossing pie · pie pan + Mountain duck = local data management

Don't put your notes and videos everywhere!

Debug details under pycharm

Chip, an empire built on sand!

芯片,建立在沙粒上的帝国!
随机推荐
jdbc-连接池
HCM 初学 ( 四 ) - 时间
输入一个表达式(用字符串表示),求这个表达式的值。
Send you through the data cloud
SystemVerilog学习-08-随机约束和线程控制
FPGA - 7系列 FPGA内部结构之Clocking -02- 时钟布线资源
[note] e-commerce order data analysis practice
TIDB数据库特性总结
Ucosiii --- engineering transplantation
Beauty of Mathematics - Application of Mathematics
OpenGL es: (1) origin of OpenGL es (transfer)
Code shoe set - mt3149 · and - the data is not very strong. Violent pruning can deceive AC
Trust guessing numbers game
Data governance: data governance framework (Part I)
OpenGL es: (2) relationship between OpenGL es, EGL and glsl
Codeforces Round #803 (Div. 2)vp
Continue to learn MySQL
3D printer threading: five simple solutions
OpenGL es: (4) detailed explanation of EGL API (Continued)
excel高级绘图技巧100讲(一)-用甘特图来展示项目进度情况