diff --git a/dsa-solutions/lc-solutions/0100-0199/0171-Excel-sheet-column-number.md b/dsa-solutions/lc-solutions/0100-0199/0171-Excel-sheet-column-number.md deleted file mode 100644 index 761f7ce39..000000000 --- a/dsa-solutions/lc-solutions/0100-0199/0171-Excel-sheet-column-number.md +++ /dev/null @@ -1,99 +0,0 @@ ---- -id: excel-sheet-column-number -title: Excel Sheet Column Number -sidebar_label: 0171-Excel Sheet Column Number -tags: [Math, String] - -description: Given a string columnTitle that represents the column title as appear in an Excel sheet, return its corresponding column number. ---- - -## Problem Statement - - -Given a string `columnTitle` that represents the column title as appear in an Excel sheet, return its corresponding column number. - -For example: -A -> 1 -B -> 2 -C -> 3 -... -Z -> 26 -AA -> 27 -AB -> 28 -... - -### Examples - -**Example 1:** - -```plaintext -Input: columnTitle = "A" -Output: 1 -``` - -**Example 2:** - -```plaintext -Input: columnTitle = "AB" -Output: 28 -``` - -**Example 3:** - -```plaintext -Input: columnTitle = "ZY" -Output: 701 -``` - -### Constraints - -- $1 \leq \text{columnTitle.length} \leq 7$ -- columnTitle consists only of uppercase English letters. -- columnTitle is in the range ["A", "FXSHRXW"]. - -## Solution - -### Approach - -To convert the Excel column title to a number, you can think of it as a base-26 number system. Each character in the string corresponds to a digit in this base-26 system, where 'A' represents 1, 'B' represents 2, ..., 'Z' represents 26. - - -#### Algorithm - -1. Initialization: -- Initialize `result` to 0. This will hold the final column number. -2. Processing Each Character: -- Iterate through each character in the columnTitle string. -- For each character, convert it to its corresponding number using `ord(char) - ord('A') + 1`. -- Multiply the current `result` by 26 and add the value of the current character. - -3. Return the Result: -- Return the final `result`. - - - -#### Implementation - -```Java -public class Solution { - public int titleToNumber(String columnTitle) { - int result = 0; - for (int i = 0; i < columnTitle.length(); i++) { - result = result * 26 + (columnTitle.charAt(i) - 'A' + 1); - } - return result; - } -} -``` - -### Complexity Analysis - -- **Time complexity**: $O(N)$ - * The time complexity is $O(N)$, where N is the length of the columnTitle string. This is because we iterate through each character in the string once. - -- **Space complexity**: $O(1)$ - * The space complexity is $O(1)$ as we are using only a constant amount of extra space. - -### Conclusion - -This solution effectively converts an Excel sheet column title into its corresponding column number using a base-26 number system approach. The time complexity is efficient, making the solution suitable for handling large input sizes. diff --git a/dsa-solutions/lc-solutions/1800-1899/1807-evaluate-the-bracket-pairs-of-a-string.md b/dsa-solutions/lc-solutions/1800-1899/1807-evaluate-the-bracket-pairs-of-a-string.md new file mode 100644 index 000000000..bdbe483e0 --- /dev/null +++ b/dsa-solutions/lc-solutions/1800-1899/1807-evaluate-the-bracket-pairs-of-a-string.md @@ -0,0 +1,192 @@ +--- +id: evaluate-the-bracket-pairs-of-a-string +title: Evaluate the Bracket Pairs of a String +sidebar_label: 1807-Evaluate the Bracket Pairs of a String +tags: + - String Manipulation + - Hash Table + - LeetCode + - Java + - Python + - C++ +description: "This is a solution to the Evaluate Bracket Pairs problem on LeetCode." +sidebar_position: 30 +--- + +## Problem Description + +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] = [keyi, valuei]` indicates that key `keyi` has a value of `valuei`. + +You are tasked to evaluate all of the bracket pairs. When you evaluate a bracket pair that contains some key `keyi`, you will: +- Replace `keyi` and the bracket pair with the key's corresponding `valuei`. +- If you do not know the value of the key, you will replace `keyi` and the bracket pair with a question mark "?" (without the quotation marks). + +Each key will appear at most once in your knowledge. There will not be any nested brackets in `s`. + +Return the resulting string after evaluating all of the bracket pairs. + +### Examples + +**Example 1:** + +``` +Input: s = "(name)is(age)yearsold", knowledge = [["name","bob"],["age","two"]] +Output: "bobistwoyearsold" +Explanation: +The key "name" has a value of "bob", so replace "(name)" with "bob". +The key "age" has a value of "two", so replace "(age)" with "two". +``` + +**Example 2:** + +``` +Input: s = "hi(name)", knowledge = [["a","b"]] +Output: "hi?" +Explanation: As you do not know the value of the key "name", replace "(name)" with "?". +``` + +**Example 3:** + +``` +Input: s = "(a)(a)(a)aaa", knowledge = [["a","yes"]] +Output: "yesyesyesaaa" +Explanation: The same key can appear multiple times. +The key "a" has a value of "yes", so replace all occurrences of "(a)" with "yes". +Notice that the "a"s not in a bracket pair are not evaluated. +``` + +### Constraints + +- `1 <= s.length <= 10^5` +- `0 <= knowledge.length <= 10^5` +- `knowledge[i].length == 2` +- `1 <= keyi.length, valuei.length <= 10` +- `s` consists of lowercase English letters and round brackets '(' and ')'. +- Every open bracket '(' in `s` will have a corresponding close bracket ')'. +- The key in each bracket pair of `s` will be non-empty. +- There will not be any nested bracket pairs in `s`. +- `keyi` and `valuei` consist of lowercase English letters. +- Each `keyi` in knowledge is unique. + +--- + +## Solution for Evaluate Bracket Pairs Problem + +To solve this problem, we need to use a hash map to store the key-value pairs from the knowledge array. Then, we can iterate through the string `s` and build the resulting string by replacing the keys within brackets with their corresponding values from the hash map. + +### Approach + +1. Create a hash map from the knowledge array. +2. Initialize a result string. +3. Traverse the string `s`: + - If we encounter a '(', start collecting the key until we find a ')'. + - Replace the key with its value from the hash map, or '?' if the key is not found. +4. Continue until the entire string is processed. + +### Code in Different Languages + + + + + +```cpp +class Solution { +public: + string evaluate(string s, vector>& knowledge) { + unordered_map map; + for (auto& pair : knowledge) { + map[pair[0]] = pair[1]; + } + + string result; + int i = 0; + while (i < s.size()) { + if (s[i] == '(') { + int j = i + 1; + while (s[j] != ')') j++; + string key = s.substr(i + 1, j - i - 1); + result += map.count(key) ? map[key] : "?"; + i = j + 1; + } else { + result += s[i++]; + } + } + return result; + } +}; +``` + + + + + +```java +class Solution { + public String evaluate(String s, List> knowledge) { + Map map = new HashMap<>(); + for (List pair : knowledge) { + map.put(pair.get(0), pair.get(1)); + } + + StringBuilder result = new StringBuilder(); + int i = 0; + while (i < s.length()) { + if (s.charAt(i) == '(') { + int j = i + 1; + while (s.charAt(j) != ')') j++; + String key = s.substring(i + 1, j); + result.append(map.getOrDefault(key, "?")); + i = j + 1; + } else { + result.append(s.charAt(i++)); + } + } + return result.toString(); + } +} +``` + + + + + +```python +class Solution: + def evaluate(self, s: str, knowledge: List[List[str]]) -> str: + knowledge_dict = {key: value for key, value in knowledge} + result = [] + i = 0 + while i < len(s): + if s[i] == '(': + j = i + 1 + while s[j] != ')': + j += 1 + key = s[i + 1:j] + result.append(knowledge_dict.get(key, "?")) + i = j + 1 + else: + result.append(s[i]) + i += 1 + return ''.join(result) +``` + + + + +#### Complexity Analysis + +- **Time Complexity**: $O(n + m)$, where `n` is the length of the string `s` and `m` is the number of key-value pairs in `knowledge`. +- **Space Complexity**: $O(m)$, for storing the hash map of knowledge. + +--- + +

Authors:

+ +
+{['ImmidiSivani'].map(username => ( + +))} +
+``` \ No newline at end of file