Skip to content

Commit 52129aa

Browse files
committed
Add optimization details
1 parent dc097fe commit 52129aa

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ def find_longest_common_prefix(strings: List[str]):
66
find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list.
77
88
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.
9+
10+
Optimisation:
11+
- Sort the list first
12+
- Similar strings end up next to each other
13+
- Compare neighbours only instead of all pairs
14+
15+
Complexity: O(n^2) -> O(n log n)
916
"""
1017

1118
if not strings or len(strings) < 2:

Sprint-2/improve_with_precomputing/count_letters/count_letters.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
def count_letters(s: str) -> int:
22
"""
33
count_letters returns the number of letters which only occur in upper case in the passed string.
4+
5+
Optimisation:
6+
- Convert string to set
7+
- Avoid checking the whole string every time
8+
9+
Complicity: O(n^2) -> O(n)
410
"""
511
chars = set(s) # precompute all characters once
612

0 commit comments

Comments
 (0)