g0701_0800.s0763_partition_labels.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-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
**Time Complexity (Big O Time):**
The program iterates through the input string `s` using a single pass. Within this pass, it performs the following operations:
1. Iterating through `s` to populate the `position` array, which records the last occurrence position of each character. This is a linear operation, O(n), where 'n' is the length of the string `s`.
2. Another linear pass through `s` to partition the string. This pass also has a time complexity of O(n).
Overall, the time complexity of the program is dominated by the linear passes through the string. Therefore, the program's time complexity is O(n), where 'n' is the length of the input string `s`.
**Space Complexity (Big O Space):**
The space complexity of the program is determined by the additional data structures used:
1. The `position` array is used to store the last occurrence position of each character. This array has a fixed size of 26 (assuming lowercase English alphabet letters only), so its space complexity is O(26), which is equivalent to O(1) because it's a constant-size array.
2. The `result` list is used to store the partition sizes, which can have a maximum length equal to the number of distinct characters in the input string `s`. In the worst case, if all characters in the alphabet are unique, this list could have a size of 26 (again assuming lowercase English alphabet letters only). Therefore, the space complexity of the `result` list is O(26), which is equivalent to O(1).
Overall, the space complexity of the program is determined by the constant-size arrays and lists, so it is O(1).
In summary, the provided program has a time complexity of O(n) and a space complexity of O(1), where 'n' is the length of the input string `s`.