LeetCode 1807: Evaluate the Bracket Pairs of a String Solution

Master LeetCode problem 1807 (Evaluate the Bracket Pairs of a String), 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.

1807. Evaluate the Bracket Pairs of a String

Problem Explanation

Explanation:

We can solve this problem using a HashMap to store the key-value pairs from the knowledge array. Then, we can iterate through the string and whenever we encounter a bracket pair, we can check if the key exists in our HashMap. If it does, we replace the bracket pair with the corresponding value; otherwise, we replace it with a question mark.

Algorithm:

  1. Create a HashMap to store the key-value pairs from the knowledge array.
  2. Iterate through the string:
    • If an opening bracket is found, extract the key until the closing bracket.
    • Check if the key exists in the HashMap.
    • Replace the bracket pair with the corresponding value or a question mark.
  3. Return the modified string.

Time Complexity:

The time complexity of this solution is O(n), where n is the length of the input string.

Space Complexity:

The space complexity is O(k), where k is the number of unique keys in the knowledge array.

:

Solution Code

class Solution {
    public String evaluate(String s, List<List<String>> knowledge) {
        Map<String, String> map = new HashMap<>();
        for (List<String> pair : knowledge) {
            map.put(pair.get(0), pair.get(1));
        }
        
        StringBuilder sb = new StringBuilder();
        int n = s.length();
        int i = 0;
        
        while (i < n) {
            char c = s.charAt(i);
            if (c == '(') {
                int j = i + 1;
                while (s.charAt(j) != ')') {
                    j++;
                }
                String key = s.substring(i + 1, j);
                sb.append(map.getOrDefault(key, "?"));
                i = j + 1;
            } else {
                sb.append(c);
                i++;
            }
        }
        
        return sb.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1807 (Evaluate the Bracket Pairs of a String)?

This page provides optimized solutions for LeetCode problem 1807 (Evaluate the Bracket Pairs of a String) 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 1807 (Evaluate the Bracket Pairs of a String)?

The time complexity for LeetCode 1807 (Evaluate the Bracket Pairs of a String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1807 on DevExCode?

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

Back to LeetCode Solutions