West Midlands | 25-SDC-July | Gabriel Deng | Sprint 2 | Improve with precomputing#72
West Midlands | 25-SDC-July | Gabriel Deng | Sprint 2 | Improve with precomputing#72gai93003 wants to merge 1 commit intoCodeYourFuture:mainfrom
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Can you explain why your solutions are better?
|
Yes of course, I can explain. For count_letters, assignment, The first thing that my code improves is the precomputation of the lowercase_letters in a set. This sets time complexity to O(n) and instead of using (if letter.lower() not in s), I am using set lookup for our precomputed set making the complexity to be O(1) per lookup. The overall complexity is O(n) instead of O(n^2) as it was for the original code. |
|
For find_longest_common_prefix Instead of comparing every pair of strings like in original code which gives a complexity of O(n^2 * m), I build the prefix dictionary once and then scan through it to find the longest prefix appearing alteast 2 times. This gives ~ O(1) for lookups. The overall complexity for this solution is O(n * m^2) which is good for many short strings |
|
Analysis looks good. Well done. |
|
Thank you. |
No description provided.