All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0020_valid_parentheses.complexity.md Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
**Time Complexity (Big O Time):**

The time complexity of this program is O(n), where "n" represents the length of the input string `s`. Here's the breakdown:

1. The program iterates through each character of the input string `s` using a `for` loop. This loop runs for "n" iterations, where "n" is the length of the string.

2. Inside the loop, the program performs constant-time operations for each character:
   - It checks if the character is one of the opening parentheses (`(`, `[`, or `{`) and pushes it onto the stack (constant time).
   - It checks if the character is one of the closing parentheses (`)`, `]`, or `}`) and verifies if it matches the corresponding opening parenthesis on the top of the stack. If they match, it pops the opening parenthesis from the stack (constant time).

3. If any character other than parentheses is encountered, the program returns `false` immediately, so the worst-case scenario is iterating through the entire string.

Since the loop runs for "n" iterations, and all operations inside the loop are constant time, the overall time complexity is O(n).

**Space Complexity (Big O Space):**

The space complexity of this program is O(n), where "n" represents the length of the input string `s`. Here's why:

1. The program uses a stack (`stack`) to keep track of the opening parentheses encountered while iterating through the string. In the worst case, when the input string contains only opening parentheses, the stack can grow to have a maximum of "n" elements, where "n" is the length of the input string.

2. Other than the stack, the program uses a few integer variables and character variables (`c`) that consume constant space relative to the input size.

Therefore, the dominant factor in terms of space complexity is the stack, which has a space complexity of O(n).




© 2015 - 2024 Weber Informatics LLC | Privacy Policy