Evaluate the Bracket Pairs of a String - Complete Solution Guide
Evaluate the Bracket Pairs of a String is LeetCode problem 1807, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a string s that contains some bracket pairs, with each pair containing a non-empty key. For example, in the string "(name)is(age)yearsold" , there are two bracket pairs that contain the keys "name" and "age" . You know the values of a wide range of keys. This is represented by a 2D string array knowledge where each knowledge[i] = [key i , value i ] indicates that key key i has a value of value i . You are tasked to evaluate all of the bracket pairs. When you evaluate a bracket pair
Detailed Explanation
The problem requires evaluating bracket pairs in a string 's' based on a 'knowledge' base. Each bracket pair contains a key. The task is to replace each bracket pair and its key with the corresponding value from the 'knowledge' base. If a key is not found in 'knowledge', it should be replaced with '?'. The 'knowledge' is a 2D array where each row contains a key-value pair. The string 's' consists of lowercase letters and round brackets. Keys are non-empty and unique within the knowledge base, and there are no nested brackets.
Solution Approach
The provided solution uses a hash table (dictionary) to store the 'knowledge' for quick lookups. It splits the input string 's' by '(' characters. It then iterates through the resulting parts. For each part, it splits it further by ')' to extract the key and the remaining part of the string. The key is then looked up in the hash table. If found, the corresponding value is appended to the result. Otherwise, '?' is appended. Finally, the remaining part of the string is appended to the result.
Step-by-Step Algorithm
- Step 1: Create a hash table (dictionary) from the 'knowledge' list to store key-value pairs for efficient lookups.
- Step 2: Split the input string 's' by the '(' character into a list of parts.
- Step 3: Initialize an empty result string with the first part (the portion before the first '(').
- Step 4: Iterate through the remaining parts (starting from the second part) of the split string.
- Step 5: For each part, split it by the ')' character to extract the 'key' and the 'rest' of the string.
- Step 6: Look up the 'key' in the hash table. If found, append its corresponding 'value' to the result string. If not found, append '?' to the result string.
- Step 7: Append the 'rest' of the string (the part after the ')') to the result string.
- Step 8: After processing all parts, return the final result string.
Key Insights
- Insight 1: Using a hash table (dictionary) to store the knowledge allows for efficient lookups of key-value pairs.
- Insight 2: Splitting the input string 's' by '(' simplifies the processing of each bracketed section.
- Insight 3: Handling the cases where the key is present or absent in the knowledge base is crucial for correctness.
Complexity Analysis
Time Complexity: O(n + k)
Space Complexity: O(k + n)
Topics
This problem involves: Array, Hash Table, String.
Companies
Asked at: Remitly.