-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0439-ternary-expression-parser.js
More file actions
38 lines (35 loc) · 1.17 KB
/
0439-ternary-expression-parser.js
File metadata and controls
38 lines (35 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* 439. Ternary Expression Parser
* https://leetcode.com/problems/ternary-expression-parser/
* Difficulty: Medium
*
* Given a string expression representing arbitrarily nested ternary expressions,
* evaluate the expression, and return the result of it.
*
* You can always assume that the given expression is valid and only contains digits,
* '?', ':', 'T', and 'F' where 'T' is true and 'F' is false. All the numbers in the
* expression are one-digit numbers (i.e., in the range [0, 9]).
*
* The conditional expressions group right-to-left (as usual in most languages), and
* the result of the expression will always evaluate to either a digit, 'T' or 'F'.
*/
/**
* @param {string} expression
* @return {string}
*/
var parseTernary = function(expression) {
const stack = [];
for (let i = expression.length - 1; i >= 0; i--) {
const char = expression[i];
if (char !== ':' && char !== '?') {
stack.push(char);
} else if (char === '?') {
const condition = expression[i - 1];
const trueValue = stack.pop();
const falseValue = stack.pop();
stack.push(condition === 'T' ? trueValue : falseValue);
i--;
}
}
return stack[0];
};