From f591dda82479f92eafe1b6d464baaad33242fc11 Mon Sep 17 00:00:00 2001 From: RahwaZeslusHaile Date: Fri, 27 Feb 2026 14:08:19 +0000 Subject: [PATCH 1/3] Optimize common_prefix and count_letters with precomputing --- .../common_prefix/common_prefix.py | 17 ++++++++--------- .../count_letters/count_letters.py | 7 +++---- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py index f4839e7..341dd8b 100644 --- a/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py +++ b/Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py @@ -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 diff --git a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py index 62c3ec0..4c7da34 100644 --- a/Sprint-2/improve_with_precomputing/count_letters/count_letters.py +++ b/Sprint-2/improve_with_precomputing/count_letters/count_letters.py @@ -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) From 345a01aae6ddf246216b0ab781c268d7e2da9345 Mon Sep 17 00:00:00 2001 From: RahwaZeslusHaile Date: Mon, 2 Mar 2026 16:34:44 +0000 Subject: [PATCH 2/3] Add complexity analysis to README explaining improvements --- Sprint-2/improve_with_precomputing/README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sprint-2/improve_with_precomputing/README.md b/Sprint-2/improve_with_precomputing/README.md index aaccd50..89c292c 100644 --- a/Sprint-2/improve_with_precomputing/README.md +++ b/Sprint-2/improve_with_precomputing/README.md @@ -3,3 +3,23 @@ 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 + +**Original Approach (Nested Loops):** +- Time Complexity: **O(n² × m)** - Compares every string with every other string (n²) pairs, each taking O(m) comparisons + +**Optimized Approach (Sort + Adjacent Pairs):** +- Time Complexity: **O(n log n + n × m)** - Sort once (n log n), then compare only adjacent pairs (n comparisons) +- **Why better:** The longest common prefix will be found in the first and last strings after sorting, eliminating unnecessary comparisons. Reduces from O(n²) to O(n) pair comparisons. + +### count_letters + +**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). From c06273960267ce3dd1fadb0951cf14eb0d12f642 Mon Sep 17 00:00:00 2001 From: RahwaZeslusHaile Date: Mon, 2 Mar 2026 17:42:03 +0000 Subject: [PATCH 3/3] Polish README: improve formatting and clarity of complexity explanations --- Sprint-2/improve_with_precomputing/README.md | 34 ++++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/Sprint-2/improve_with_precomputing/README.md b/Sprint-2/improve_with_precomputing/README.md index 89c292c..645dc39 100644 --- a/Sprint-2/improve_with_precomputing/README.md +++ b/Sprint-2/improve_with_precomputing/README.md @@ -8,15 +8,43 @@ Speed up the solutions by introducing precomputing. You may rewrite as much of t ### 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 (n²) pairs, each taking O(m) comparisons +- **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(n log n + n × m)** - Sort once (n log n), then compare only adjacent pairs (n comparisons) -- **Why better:** The longest common prefix will be found in the first and last strings after sorting, eliminating unnecessary comparisons. Reduces from O(n²) to O(n) pair comparisons. +- **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