diff --git a/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/Explanation.md b/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/Explanation.md new file mode 100644 index 0000000..8fa7e45 --- /dev/null +++ b/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/Explanation.md @@ -0,0 +1,90 @@ +# 1021. Remove Outermost Parentheses + +**Difficulty:** *Easy* +**Category:** *Strings, Stack, Greedy* +**Leetcode Link:** [Problem Link](https://leetcode.com/problems/remove-outermost-parentheses/) + +--- + +## πŸ“ Introduction + +*Given a valid parentheses string, the task is to remove the outermost parentheses of every primitive substring. A primitive string is a non-empty valid parentheses string that cannot be split further into valid parts.* + +--- + +## πŸ’‘ Approach & Key Insights + +*Each primitive parentheses string is balanced and starts/ends with outermost parentheses. If we track the nesting level using a counter, we can easily identify the start and end of each primitive and skip appending the outermost layer.* + +--- + +## πŸ› οΈ Breakdown of Approaches + +### 1️⃣ Brute Force / Naive Approach + +- **Explanation:** *Use a stack to build substrings for each primitive, detect primitives by ensuring stack becomes empty, then remove the first and last characters.* +- **Time Complexity:** *O(nΒ²) - Substring operations inside loop.* +- **Space Complexity:** *O(n) - For temporary stack and result string.* +- **Example/Dry Run:** + +Example input: "(()())(())" +Step 1 β†’ Extract primitives: "(()())", "(())" +Step 2 β†’ Remove outermost: "()()", "()" +Step 3 β†’ Output: "()()()" + +### 2️⃣ Optimized Approach + +- **Explanation:** *Use a counter to track the nesting depth. While iterating, append characters only if they’re not the outermost (i.e., depth > 1 before β€˜)’ or after β€˜(’).* +- **Time Complexity:** *O(n) - Single pass over string.* +- **Space Complexity:** *O(n) - For output string.* +- **Example/Dry Run:** + +Example input: "(()())(())" +Step 1 β†’ Traverse: +- β€˜(’: depth becomes 1 β†’ don’t add +- β€˜(’: depth becomes 2 β†’ add β€˜(’ +- β€˜)’: depth becomes 1 β†’ add β€˜)’ +- β€˜)’: depth becomes 0 β†’ don’t add +... and so on +Output: "()()()" + +--- + +## πŸ“Š Complexity Analysis + +| Approach | Time Complexity | Space Complexity | +| ------------- | --------------- | ---------------- | +| Brute Force | O(nΒ²) | O(n) | +| Optimized | O(n) | O(n) | + +--- + +## πŸ“‰ Optimization Ideas + +*The optimized solution is already linear in time and space. Further improvement would require in-place modification or custom output building if language permits.* + +--- + +## πŸ“Œ Example Walkthroughs & Dry Runs + +```plaintext +Example: +Input: "(()())(())(()(()))" +Process: +Primitive 1: "(()())" β†’ Output: "()()" +Primitive 2: "(())" β†’ Output: "()" +Primitive 3: "(()(()))" β†’ Output: "()(())" +Final Output: "()()()()(())" +``` + +--- + +## πŸ”— Additional Resources + +- [Stack Data Structure](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) +- [Valid Parentheses Leetcode](https://leetcode.com/problems/valid-parentheses/) + +--- + +Author: Abdul Wahab +Date: 19/07/2025 diff --git a/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/removeOuterParentheses.cpp b/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/removeOuterParentheses.cpp new file mode 100644 index 0000000..67416c0 --- /dev/null +++ b/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/removeOuterParentheses.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + string removeOuterParentheses(string s) { + string res; + int balance = 0, start = 0; + for (int i = 0; i < s.length(); ++i) { + if (s[i] == '(') balance++; + else balance--; + if (balance == 0) { + res += s.substr(start + 1, i - start - 1); + start = i + 1; + } + } + return res; + } +}; diff --git a/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/removeOuterParentheses.py b/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/removeOuterParentheses.py new file mode 100644 index 0000000..1a87f35 --- /dev/null +++ b/Arrays & Strings/#1021 - Remove outer most parantheses - Easy/removeOuterParentheses.py @@ -0,0 +1,15 @@ +class Solution: + def removeOuterParentheses(self, s: str) -> str: + res = [] + balance = 0 + start = 0 + + for i, ch in enumerate(s): + if ch == '(': + balance += 1 + else: + balance -= 1 + if balance == 0: + res.append(s[start + 1:i]) + start = i + 1 + return ''.join(res) \ No newline at end of file