Valid Coordinate Pairs ā Problem Statement & Solution Guide
Problem Description
Given a string of coordinates in the format of (x, y), determine if the pairs of coordinates are properly nested. Every open parenthesis must have a corresponding close parenthesis.
Examples
Input
( ( ) ) ( )
Output
true
Explanation: Step-by-step: 1. Initialize an empty stack. 2. Iterate through the string. When we encounter an open parenthesis, push it onto the stack. When we encounter a close parenthesis, check if the stack is empty. If it is, return false because there's an extra closing parenthesis. If it's not, pop the open parenthesis from the stack. 3. After iterating through the string, if the stack is empty, return true because all parentheses are properly nested. If the stack is not empty, return false because there are multiple open parentheses before a close parenthesis.
Input
( ( ) ) ( ) (
Output
false
Explanation: Step-by-step: 1. Initialize an empty stack. 2. Iterate through the string. When we encounter an open parenthesis, push it onto the stack. When we encounter a close parenthesis, check if the stack is empty. If it is, return false because there's an extra closing parenthesis. If it's not, pop the open parenthesis from the stack. 3. After iterating through the string, if the stack is empty, return true because all parentheses are properly nested. If the stack is not empty, return false because there are multiple open parentheses before a close parenthesis.
Constraints
- The input string will contain at most 1000 sets of coordinates.
- Each set of coordinates will be in the format of (latitude, longitude) where latitude and longitude are decimal numbers between -90 and 90 and -180 and 180 respectively.
Optimal Approach & Strategy
The optimal approach involves using a stack data structure to keep track of the opening parentheses, allowing for a time complexity of O(n) and a space complexity of O(n). This approach is efficient and scalable for large inputs.
Brute Force Approach
A brute-force approach would involve checking every possible subset of the input string to determine if the coordinates are properly nested, resulting in a time complexity of O(n²). This approach is inefficient and not scalable for large inputs. It can be improved by using a more efficient data structure.
Verified Code Solutions
function validCoordinatePairs(coordinates) {
let stack = [];
for (let coordinate of coordinates) {
if (coordinate.startsWith('(')) {
stack.push(coordinate);
} else if (coordinate.startsWith(')')) {
while (stack.length > 0 && !stack[stack.length - 1].startsWith('(')) {
stack.pop();
}
if (stack.length === 0 || !stack[stack.length - 1].startsWith('(')) {
return false;
}
stack.pop();
}
}
return stack.length === 0 && coordinates.length === stack.length;
}public boolean validCoordinatePairs(String coordinates) {
Stack<Character> stack = new Stack<>();
for (char c : coordinates.toCharArray()) {
if (c == '(') {
stack.push(c);
} else if (c == ')') {
if (stack.isEmpty()) {
return false;
}
stack.pop();
}
}
return stack.isEmpty();
}def valid_coordinate_pairs(coordinates: str) -> bool:
stack = []
for char in coordinates:
if char == '(':
stack.append(char)
elif char == ')':
if not stack:
return False
stack.pop()
return not stackfunction validCoordinatePairs(coordinates) {
let stack = [];
for (let coordinate of coordinates) {
if (coordinate.startsWith('(')) {
stack.push(coordinate);
} else if (coordinate.startsWith(')')) {
while (stack.length > 0 && !stack[stack.length - 1].startsWith('(')) {
stack.pop();
}
if (stack.length === 0 || !stack[stack.length - 1].startsWith('(')) {
return false;
}
stack.pop();
}
}
return stack.length === 0 && coordinates.length === stack.length;
}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.