g0601_0700.s0647_palindromic_substrings.complexity.md Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
**Time Complexity (Big O Time):**
The program uses a loop that iterates through each character in the input string `s`. For each character, it calls the `expand` function twice:
1. The `expand` function expands around a single character, so it has a linear time complexity of O(n), where 'n' is the length of the input string `s`.
2. The `expand` function expands around a pair of characters (centered at `i` and `i+1`), so it also has a linear time complexity of O(n).
Since both expansions are performed for each character in the input string `s`, the overall time complexity of the program is O(n^2), where 'n' is the length of the input string.
**Space Complexity (Big O Space):**
The space complexity of the program is determined by the additional data structures used, including the character array `a` and the integer array `res`.
1. The character array `a` stores a copy of the input string `s`, which requires O(n) space, where 'n' is the length of the input string.
2. The integer array `res` is used to store the result, and it requires constant space, O(1).
Therefore, the overall space complexity of the program is O(n) due to the character array `a`.
In summary, the provided program has a time complexity of O(n^2) and a space complexity of O(n), where 'n' is the length of the input string `s`. It efficiently counts the number of palindromic substrings by expanding around each character and each pair of characters in the string.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy