Valid Parentheses Solution for Accenture

Company: Accenture · Difficulty: Easy · Topics: String, Stack

Problem Statement

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

1. Open brackets must be closed by the same type of brackets.

2. Open brackets must be closed in the correct order.

3. Every close bracket has a corresponding open bracket of the same type.

Key Understanding:

Examples

Example 1:

Input: s = "()"

Output: true

Explanation:

Example 2:

Input: s = "()[]{}"

Output: true

Explanation:

Example 3:

Input: s = "(]"

Output: false

Explanation:

Example 4:

Input: s = "([)]"

Output: false

Explanation:

Example 5:

Input: s = "{[]}"

Output: true

Explanation:

Intuition

The key insight is that we need to match each closing bracket with its corresponding opening bracket. The last opening bracket should be closed first (LIFO - Last In, First Out), which suggests using a stack.

Core Concept:

Why Stack Works:

Approach

Approach 1: Stack (Optimal)

Algorithm:

1. Initialize an empty stack

2. For each character in the string:

3. At the end, check if stack is empty (if not, return false)

4. Return true if all checks pass

Why This Works:

Complexity Analysis:

Approach 2: Array as Stack

Algorithm:

1. Use an array to simulate stack operations

2. Keep track of stack size manually

3. Same logic as stack approach but with array implementation

Why This Works:

Complexity Analysis:

Approach 3: Character Counting (Limited)

Algorithm:

1. Count opening and closing brackets separately

2. Check if counts are equal for each bracket type

3. Only works for simple cases, fails for nested brackets

Why This Doesn't Work:

Complexity Analysis:

Dry Run

Let's trace through Example 1: s = "()"

Stack Approach:

Initial State: stack = [], i = 0

Step 1: i = 0, char = '('

Step 2: i = 1, char = ')'

Step 3: End of string

Example 4: s = "([)]"

Initial State: stack = [], i = 0

Step 1: i = 0, char = '('

Step 2: i = 1, char = '['

Step 3: i = 2, char = ')'

Key Insights

1. LIFO Principle: Stack naturally implements the required LIFO behavior

2. Matching Pairs: Each closing bracket must match its corresponding opening bracket

3. Nesting Order: Brackets must be properly nested (cannot close outer bracket before inner)

4. Stack Size: Maximum stack size is n/2 (when all characters are opening brackets)

5. Early Termination: Can return false as soon as a mismatch is found

Edge Cases

1. Empty String: Should return true (no brackets to validate)

2. Single Opening Bracket: Should return false (no closing bracket)

3. Single Closing Bracket: Should return false (no opening bracket)

4. Unmatched Opening Brackets: Should return false (stack not empty at end)

5. Unmatched Closing Brackets: Should return false (stack empty when closing bracket encountered)

6. Mixed Valid/Invalid: Should return false as soon as first mismatch is found

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Stack (Optimal)

Time Complexity: O(n) - We traverse the string once

Space Complexity: O(n) - We store at most n/2 opening brackets

Advantages:

Disadvantages:

Solution 2: Array as Stack

Time Complexity: O(n) - We traverse the string once

Space Complexity: O(n) - We store at most n/2 opening brackets

Advantages:

Disadvantages:

Solution 3: Character Counting (Limited)

Time Complexity: O(n) - We traverse the string once

Space Complexity: O(1) - We use only constant space

Advantages:

Disadvantages:

More Accenture coding questions