g0001_0100.s0072_edit_distance.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
**Time Complexity (Big O Time):**
1. The program uses dynamic programming with a nested loop structure. The outer loop runs from 1 to `n1`, and the inner loop runs from 1 to `n2`, where `n1` and `n2` are the lengths of the input words `w1` and `w2`.
2. Inside the inner loop, there are constant-time operations like array indexing, character comparisons, and mathematical operations (addition and minimum calculations). These operations do not depend on the size of the input words.
3. Therefore, the dominant factor in the time complexity is the nested loops. The outer loop runs for `n1` iterations, and the inner loop runs for `n2` iterations. As a result, the time complexity is O(n1 * n2).
4. In the worst case, `n1` and `n2` can be equal to the lengths of the input words, so the time complexity can be represented as O(n^2), where 'n' is the maximum of the lengths of `w1` and `w2`.
**Space Complexity (Big O Space):**
1. The program uses an integer array `dp` of size `n2 + 1`, where `n2` is the length of the shorter word `w2`. Additionally, there are some integer variables used for temporary storage.
2. Therefore, the space complexity is O(n2) because the space usage is directly proportional to the length of the shorter input word `w2`.
3. The space complexity does not depend on the length of the longer word `w1` because the program is designed to handle the shorter word as the outer loop variable.
In summary, the time complexity of the provided program is O(n^2), where 'n' is the maximum of the lengths of `w1` and `w2`. The space complexity is O(n2), where `n2` is the length of the shorter word `w2`. The program efficiently calculates the minimum edit distance between two words using dynamic programming.