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

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

The newest version!
**Time Complexity (Big O Time):**

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

1. The `cache` is a 2D Boolean array with dimensions (s.length() + 1) x (p.length() + 1). Therefore, it has O(m*n) space complexity.

2. The program uses dynamic programming and recursion to determine if the given strings `s` and `p` match. The primary function `isMatch` is called with varying indices `i` and `j` within the ranges [0, m] and [0, n], respectively.

3. The `cache` array is used to memoize the results of subproblems to avoid redundant computations. If the result for a specific `(i, j)` pair is already in the cache, it's retrieved in constant time.

4. The recursion, however, explores all possible combinations of `i` and `j` values. In the worst case, it explores all possible pairs within the range [0, m] and [0, n], resulting in a time complexity of O(m*n).

Therefore, the overall time complexity is O(m*n).

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

The space complexity of this program is O(m*n), primarily due to the `cache` 2D Boolean array. Additionally, there's a negligible amount of space used for other variables, such as `result`, `firstMatch`, `i`, and `j`, which are all constants and do not depend on the input size.

Hence, the dominant factor in terms of space complexity is the `cache` array, which has O(m*n) space complexity.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy