Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@ def find_longest_common_prefix(strings: List[str]):

In the event that an empty list, a list containing one string, or a list of strings with no common prefixes is passed, the empty string will be returned.
"""
if len(strings) < 2:
return ""

strings = sorted(strings)

longest = ""
for string_index, string in enumerate(strings):
for other_string in strings[string_index+1:]:
common = find_common_prefix(string, other_string)
if len(common) > len(longest):
longest = common

for i in range(len(strings) - 1):
common = find_common_prefix(strings[i], strings[i + 1])

if len(common) > len(longest):
longest = common

Comment on lines -11 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can you use complexity to explain how the new implementation is better than the original implementation?

@pathywang pathywang Oct 10, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks of course. The original implementation compares every possible pair of strings, resulting in O(n²) pair comparisons. The updated implementation sorts the strings and compares only adjacent pairs, reducing the number of prefix comparisons to O(n). Sorting takes O(n log n) comparisons, so the overall time complexity is O(n log n + n × m), where n is the number of strings and m is the maximum string length. It also avoids repeatedly creating list slices, reducing unnecessary memory allocations. Is it OK?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If string length, $m$, is to be taken into consideration, how does $m$ affect the complexity in the original implementation and the complexity of sorting?

@pathywang pathywang Oct 10, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks. The maximum string length, m, affects the cost of comparing strings because finding their common prefix can take O(m) time in the worst case. In the original implementation, there are O(n²) pairs to compare, so the overall time complexity is O(n² × m). In the updated implementation, sorting n strings takes O(n × m × log n) in the worst case because each string comparison can take O(m). Comparing adjacent strings afterwards takes O(n × m). Therefore, the overall worst-case time complexity of the updated implementation is O(n × m × log n), which is an improvement over O(n² × m). Both implementations depend on the string length m, but your new implementation reduces the number of string comparisons from quadratic growth to sorting-level growth. So I think the complexity of sorting is better compared with the original implementation

return longest


def find_common_prefix(left: str, right: str) -> str:
min_length = min(len(left), len(right))

for i in range(min_length):
if left[i] != right[i]:
return left[:i]

return left[:min_length]

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ def count_letters(s: str) -> int:
"""
count_letters returns the number of letters which only occur in upper case in the passed string.
"""
letters = set(s)
only_upper = set()
for letter in s:
for letter in letters:
if is_upper_case(letter):
if letter.lower() not in s:
if letter.lower() not in letters:
only_upper.add(letter)
return len(only_upper)


def is_upper_case(letter: str) -> bool:
return letter == letter.upper()

Loading