Skip to content

Commit e6642ab

Browse files
committed
use precomputing for better performance
1 parent e718fb4 commit e6642ab

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ def find_longest_common_prefix(strings: List[str]):
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.
99
"""
10+
if len(strings) < 2:
11+
return ""
12+
13+
strings = sorted(strings)
1014
longest = ""
11-
for string_index, string in enumerate(strings):
12-
for other_string in strings[string_index+1:]:
13-
common = find_common_prefix(string, other_string)
14-
if len(common) > len(longest):
15-
longest = common
15+
16+
for string_index in range(len(strings) - 1):
17+
string = strings[string_index]
18+
other_string = strings[string_index + 1]
19+
common = find_common_prefix(string, other_string)
20+
if len(common) > len(longest):
21+
longest = common
22+
1623
return longest
1724

1825

@@ -21,4 +28,4 @@ def find_common_prefix(left: str, right: str) -> str:
2128
for i in range(min_length):
2229
if left[i] != right[i]:
2330
return left[:i]
24-
return left[:min_length]
31+
return left[:min_length]
Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
def count_letters(s: str) -> int:
2-
"""
3-
count_letters returns the number of letters which only occur in upper case in the passed string.
4-
"""
5-
only_upper = set()
2+
upper_letters = set()
3+
lower_letters = set()
4+
65
for letter in s:
7-
if is_upper_case(letter):
8-
if letter.lower() not in s:
9-
only_upper.add(letter)
10-
return len(only_upper)
6+
if letter.isupper():
7+
upper_letters.add(letter)
8+
elif letter.islower():
9+
lower_letters.add(letter)
1110

11+
count = 0
12+
for letter in upper_letters:
13+
if letter.lower() not in lower_letters:
14+
count += 1
1215

13-
def is_upper_case(letter: str) -> bool:
14-
return letter == letter.upper()
16+
return count

0 commit comments

Comments
 (0)