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
21 changes: 11 additions & 10 deletions Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
from typing import List


def find_longest_common_prefix(strings: List[str]):
def find_longest_common_prefix(string_list: List[str]):
"""
find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list.

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.
"""
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
return longest
if len(string_list) < 2:
return ""
# Precompute: sort the strings so common prefixes are adjacent
sorted_strings = sorted(string_list)
longest_prefix = ""
for index in range(len(sorted_strings) - 1):
common_prefix = find_common_prefix(sorted_strings[index], sorted_strings[index + 1])
if len(common_prefix) > len(longest_prefix):
longest_prefix = common_prefix
return longest_prefix


def find_common_prefix(left: str, right: str) -> str:
Expand Down
20 changes: 10 additions & 10 deletions Sprint-2/improve_with_precomputing/count_letters/count_letters.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
def count_letters(s: str) -> int:
def count_letters(text: str) -> int:
"""
count_letters returns the number of letters which only occur in upper case in the passed string.
"""
only_upper = set()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is the key precomputing step. By building lower_set once here (O(n)), you turn every subsequent membership check into O(1) instead of O(n). The original code did letter.lower() not in s where s is a plain string — Python's in operator on strings is a substring search and costs O(n) per call, making the old function O(n²) overall. Your approach is a textbook example of precomputing a lookup structure.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

So, what is a better way to write it?

for letter in s:
if is_upper_case(letter):
if letter.lower() not in s:
only_upper.add(letter)
# Find all lowercase letters in the string
lower_set = set(char for char in text if char.islower())

# Find all uppercase letters in the string
upper_set = set(char for char in text if char.isupper())

# Count uppercase letters that don't have lowercase versions
only_upper = {char for char in upper_set if char.lower() not in lower_set}

return len(only_upper)


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