LeetCode 439: Ternary Expression Parser Solution

Master LeetCode problem 439 (Ternary Expression Parser), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

439. Ternary Expression Parser

Problem Explanation

Explanation:

This problem can be solved using a recursive approach. We start by iterating through the expression from right to left. Whenever we encounter a '?', we know that we have a ternary expression. We evaluate the condition before the '?' to decide which expression to choose. If the condition is true, we recursively evaluate the expression before the ':', otherwise we recursively evaluate the expression after the ':'. We continue this process until we reach the end of the expression.

Algorithm:

  1. Start iterating the expression from right to left.
  2. If we encounter a '?', recursively evaluate the expression before the '?' if the condition is true, otherwise recursively evaluate the expression after the ':'.
  3. Keep track of the current index while iterating.
  4. Return the final result.

Time Complexity: O(n), where n is the length of the expression. Space Complexity: O(n), for the recursive call stack.

: :

Solution Code

class Solution {
    public String parseTernary(String expression) {
        if (expression == null || expression.length() == 0) {
            return "";
        }
        
        return evaluate(expression, 0, expression.length() - 1);
    }
    
    private String evaluate(String expression, int start, int end) {
        if (start == end) {
            return expression.substring(start, end + 1);
        }
        
        int count = 0;
        int i = end;
        
        while (i >= start) {
            if (expression.charAt(i) == '?') {
                count++;
            } else if (expression.charAt(i) == ':') {
                count--;
                if (count == 0) {
                    break;
                }
            }
            i--;
        }
        
        return expression.charAt(start) == 'T' ? evaluate(expression, start + 2, i - 1) : evaluate(expression, i + 1, end);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 439 (Ternary Expression Parser)?

This page provides optimized solutions for LeetCode problem 439 (Ternary Expression Parser) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 439 (Ternary Expression Parser)?

The time complexity for LeetCode 439 (Ternary Expression Parser) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 439 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 439 in Java, C++, or Python.

Back to LeetCode Solutions