Skip to content

Comments

West Midlands | 25-SDC-July | Gabriel Deng | Sprint 2 | Improve with precomputing#72

Open
gai93003 wants to merge 1 commit intoCodeYourFuture:mainfrom
gai93003:improve_with_precomputing
Open

West Midlands | 25-SDC-July | Gabriel Deng | Sprint 2 | Improve with precomputing#72
gai93003 wants to merge 1 commit intoCodeYourFuture:mainfrom
gai93003:improve_with_precomputing

Conversation

@gai93003
Copy link

No description provided.

@gai93003 gai93003 added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. Module-Complexity The name of the module. labels Jan 13, 2026
Copy link

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why your solutions are better?

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Jan 23, 2026
@gai93003
Copy link
Author

gai93003 commented Feb 21, 2026

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.
~ This improves the process especially if we are working with long strings with alot of uppercase letters.

The overall complexity is O(n) instead of O(n^2) as it was for the original code.

@gai93003
Copy link
Author

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

@cjyuan
Copy link

cjyuan commented Feb 21, 2026

Analysis looks good. Well done.

@cjyuan cjyuan added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Feb 21, 2026
@gai93003
Copy link
Author

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. Module-Complexity The name of the module. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants