BackmediumStackPayPal

Valid Coordinate Pairs Solution

Problem Statement

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.

Example 1
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.

Example 2
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.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Valid Coordinate Pairs — Problem Statement & Solution Guide

StackMediummatching parentheses
TimeO(n)
|
SpaceO(1)

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

Example 1

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.

Example 2

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

JavaScript Solution
Time: O(n)
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;
}

Asked in Top Tech Interviews

PayPal

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.