Skip to content

Commit 3d9ce82

Browse files
committed
New Problem Solution -"Evaluate the Bracket Pairs of a String"
1 parent e2aa8b1 commit 3d9ce82

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LeetCode
99

1010
| # | Title | Solution | Difficulty |
1111
|---| ----- | -------- | ---------- |
12+
|1807|[Evaluate the Bracket Pairs of a String](https://leetcode.com/problems/evaluate-the-bracket-pairs-of-a-string/) | [C++](./algorithms/cpp/evaluateTheBracketPairsOfAString/EvaluateTheBracketPairsOfAString.cpp)|Medium|
1213
|1806|[Minimum Number of Operations to Reinitialize a Permutation](https://leetcode.com/problems/minimum-number-of-operations-to-reinitialize-a-permutation/) | [C++](./algorithms/cpp/minimumNumberOfOperationsToReinitializeAPermutation/MinimumNumberOfOperationsToReinitializeAPermutation.cpp)|Medium|
1314
|1805|[Number of Different Integers in a String](https://leetcode.com/problems/number-of-different-integers-in-a-string/) | [C++](./algorithms/cpp/numberOfDifferentIntegersInAString/NumberOfDifferentIntegersInAString.cpp)|Easy|
1415
|1803|[Count Pairs With XOR in a Range](https://leetcode.com/problems/count-pairs-with-xor-in-a-range/) | [C++](./algorithms/cpp/countPairsWithXorInARange/CountPairsWithXorInARange.cpp)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Source : https://leetcode.com/problems/evaluate-the-bracket-pairs-of-a-string/
2+
// Author : Hao Chen
3+
// Date : 2021-03-28
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given a string s that contains some bracket pairs, with each pair containing a non-empty
8+
* key.
9+
*
10+
* For example, in the string "(name)is(age)yearsold", there are two bracket pairs that
11+
* contain the keys "name" and "age".
12+
*
13+
* You know the values of a wide range of keys. This is represented by a 2D string array knowledge
14+
* where each knowledge[i] = [keyi, valuei] indicates that key keyi has a value of valuei.
15+
*
16+
* You are tasked to evaluate all of the bracket pairs. When you evaluate a bracket pair that contains
17+
* some key keyi, you will:
18+
*
19+
* Replace keyi and the bracket pair with the key's corresponding valuei.
20+
* If you do not know the value of the key, you will replace keyi and the bracket pair with a
21+
* question mark "?" (without the quotation marks).
22+
*
23+
* Each key will appear at most once in your knowledge. There will not be any nested brackets in s.
24+
*
25+
* Return the resulting string after evaluating all of the bracket pairs.
26+
*
27+
* Example 1:
28+
*
29+
* Input: s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]]
30+
* Output: "bobistwoyearsold"
31+
* Explanation:
32+
* The key "name" has a value of "bob", so replace "(name)" with "bob".
33+
* The key "age" has a value of "two", so replace "(age)" with "two".
34+
*
35+
* Example 2:
36+
*
37+
* Input: s = "hi(name)", knowledge = [["a","b"]]
38+
* Output: "hi?"
39+
* Explanation: As you do not know the value of the key "name", replace "(name)" with "?".
40+
*
41+
* Example 3:
42+
*
43+
* Input: s = "(a)(a)(a)aaa", knowledge = [["a","yes"]]
44+
* Output: "yesyesyesaaa"
45+
* Explanation: The same key can appear multiple times.
46+
* The key "a" has a value of "yes", so replace all occurrences of "(a)" with "yes".
47+
* Notice that the "a"s not in a bracket pair are not evaluated.
48+
*
49+
* Example 4:
50+
*
51+
* Input: s = "(a)(b)", knowledge = [["a","b"],["b","a"]]
52+
* Output: "ba"
53+
*
54+
* Constraints:
55+
*
56+
* 1 <= s.length <= 10^5
57+
* 0 <= knowledge.length <= 10^5
58+
* knowledge[i].length == 2
59+
* 1 <= keyi.length, valuei.length <= 10
60+
* s consists of lowercase English letters and round brackets '(' and ')'.
61+
* Every open bracket '(' in s will have a corresponding close bracket ')'.
62+
* The key in each bracket pair of s will be non-empty.
63+
* There will not be any nested bracket pairs in s.
64+
* keyi and valuei consist of lowercase English letters.
65+
* Each keyi in knowledge is unique.
66+
******************************************************************************************************/
67+
68+
class Solution {
69+
private:
70+
bool isBracket(char c) {
71+
return c=='(' || c == ')';
72+
}
73+
public:
74+
string evaluate(string s, vector<vector<string>>& knowledge) {
75+
unordered_map<string, string> dict;
76+
for(auto& k : knowledge) {
77+
dict[k[0]] = k[1];
78+
}
79+
80+
string result;
81+
string key;
82+
bool meetLeftBracket = false;
83+
for(auto& c : s) {
84+
85+
if (c == '(') {
86+
meetLeftBracket = true;
87+
} else if (c == ')') {
88+
meetLeftBracket = false;
89+
//cout << key << endl;
90+
if (dict.find(key) != dict.end()) {
91+
result += dict[key];
92+
}else {
93+
result += '?';
94+
}
95+
key = "";
96+
} else {
97+
if (meetLeftBracket) {
98+
key += c;
99+
}else{
100+
result +=c;
101+
}
102+
}
103+
}
104+
105+
return result;
106+
}
107+
};

0 commit comments

Comments
 (0)