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
48 changes: 48 additions & 0 deletions Sprint-2/improve_with_precomputing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,51 @@ Each directory contains implementations that can be made faster by using pre-com
The problem they solve is described in docstrings and tests.

Speed up the solutions by introducing precomputing. You may rewrite as much of the solution as you want, and may add any tests, but must not modify existing tests.

## Improvements Made

### common_prefix

**Problem:** Find the longest common prefix among any two strings in a list.

**Original Approach (Nested Loops):**
- **Time Complexity:** O(n² × m)
- Compares every string with every other string: O(n²) pairs
- Each string comparison takes O(m), where m is the average string length
- Total: n² string comparisons, each O(m)

**Optimized Approach (Sort + Adjacent Pairs):**
- **Time Complexity:** O(nm log n)
- Sorting n strings: O(nm log n) where each comparison is O(m)
- Comparing adjacent pairs: O(nm) for n-1 comparisons
- Dominated by sorting: O(nm log n)

**Why Better:** After sorting, strings with common prefixes become adjacent. We only need to compare n-1 adjacent pairs instead of all n² possible pairs. This reduces the number of pair comparisons from O(n²) to O(n), making it significantly faster for large inputs.

---

### count_letters

**Problem:** Count letters that only occur in uppercase in a string.

**Original Approach (Repeated String Searches):**
- **Time Complexity:** O(n²)
- For each character: check if `letter.lower() in s` → O(n) search
- Repeated for n characters → O(n²) total

**Optimized Approach (Precomputed Set):**
- **Time Complexity:** O(n)
- Build set of lowercase letters: O(n)
- Check each uppercase letter against set: O(1) per lookup
- Total: O(n) + O(n) = O(n)

**Why Better:** By precomputing all lowercase letters into a set once (O(n)), we replace n repeated O(n) string searches with O(1) set lookups. This reduces the overall complexity from O(n²) to O(n).

Why better: Replaces repeated O(n) string searches with single O(n) precomputation and O(1) set lookups, reducing overall complexity from O(n²) to O(n).

**Original Approach (Repeated String Checks):**
- Time Complexity: **O(n²)** - For each letter, checking if lowercase version exists in string is O(n), done n times

**Optimized Approach (Precomputed Set):**
- Time Complexity: **O(n)** - Precompute lowercase letters into a set (O(n)), then check membership is O(1) each time
- **Why better:** Replaces repeated O(n) string searches with single O(n) precomputation and O(1) set lookups, reducing overall from O(n²) to O(n).
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@


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

if len(strings) < 2:
return ""

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.
"""
sorted_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 index in range(len(sorted_strings) - 1):
common = find_common_prefix(sorted_strings[index], sorted_strings[index + 1])
if len(common) > len(longest):
longest = common
return longest


Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
def count_letters(s: str) -> int:
"""
count_letters returns the number of letters which only occur in upper case in the passed string.
"""

lower_letters = {letter for letter in s if letter.islower()}
only_upper = set()
for letter in s:
if is_upper_case(letter):
if letter.lower() not in s:
if letter.lower() not in lower_letters:
only_upper.add(letter)
return len(only_upper)

Expand Down