Bracket Sequence Validator ā Problem Statement & Solution Guide
Problem Description
Given a string of bracket characters, implement a function to determine if the sequence is balanced.
Examples
Input
{[]}Output
false
Explanation: Step-by-step: The input '{[]}' does not contain any HTML tags. We start by checking the opening brackets. We have '{' which is an opening bracket. Then we have '[' which is also an opening bracket. However, we do not have any corresponding closing brackets for these opening brackets. Therefore, the sequence is not balanced and the output is false.
Input
({[]})Output
false
Explanation: Step-by-step: The input '({[]})' does not contain any HTML tags. We start by checking the opening brackets. We have '(' which is an opening bracket. Then we have '{' which is also an opening bracket. Then we have '[' which is an opening bracket. Then we have '}' which is a closing bracket for '{'. Then we have ']' which is a closing bracket for '['. Finally, we have ')' which is a closing bracket for '('. Therefore, the sequence is balanced and the output is true. However, the problem statement does not handle HTML tags and the output should be false.
Constraints
- The input sequence will contain at most 1000 HTML tags.
- The input sequence will only contain the following HTML tags: <html>, <body>, <h1>, <p>.
Optimal Approach & Strategy
An optimized approach would use a stack to keep track of the opening tags, allowing for a time complexity of O(n) and a space complexity of O(n).
Brute Force Approach
A brute-force approach would involve checking every possible subsequence of the input to see if it is valid, resulting in a time complexity of O(n²). This approach is inefficient and would not be suitable for large inputs.
Verified Code Solutions
function isBalanced(s) { let stack = []; let bracketMap = {')': '(', '}': '{', ']': '['}; for (let i = 0; i < s.length; i++) { if (s[i] === '(' || s[i] === '{' || s[i] === '[') { stack.push(s[i]); } else if (s[i] === ')' || s[i] === '}' || s[i] === ']') { if (stack.length === 0 || bracketMap[s[i]] !== stack.pop()) { return false; } } } return stack.length === 0; }public boolean bracketSequenceValidator(String s) {
Stack<Character> stack = new Stack<>();
Map<Character, Character> bracketMap = new HashMap<>();
bracketMap.put(')', '(');
bracketMap.put('}', '{');
bracketMap.put(']', '[');
for (char c : s.toCharArray()) {
if (bracketMap.containsValue(c)) {
stack.push(c);
} else if (bracketMap.containsKey(c)) {
if (stack.isEmpty() || bracketMap.get(c) != stack.pop()) {
return false;
}
}
}
return stack.isEmpty();
}def bracket_sequence_validator(s: str) -> bool:
stack = []
bracket_map = {')': '(', '}': '{', ']': '['}
for c in s:
if c in bracket_map.values():
stack.append(c)
elif c in bracket_map.keys():
if not stack or bracket_map[c] != stack.pop():
return False
return not stackfunction isBalanced(s) { let stack = []; let bracketMap = {')': '(', '}': '{', ']': '['}; for (let i = 0; i < s.length; i++) { if (s[i] === '(' || s[i] === '{' || s[i] === '[') { stack.push(s[i]); } else if (s[i] === ')' || s[i] === '}' || s[i] === ']') { if (stack.length === 0 || bracketMap[s[i]] !== stack.pop()) { return false; } } } return stack.length === 0; }Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.