Skip to content

Commit 035d60e

Browse files
committed
refactored
1 parent e718fb4 commit 035d60e

2 files changed

Lines changed: 18 additions & 19 deletions

File tree

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ 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-
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
16-
return longest
10+
if len(strings) < 2:
11+
return ""
12+
13+
sorted_strings = sorted(strings)
14+
first = sorted_strings[0]
15+
last = sorted_strings[-1]
16+
min_length = min(len(first), len(last))
17+
i = 0
18+
while i < min_length and first[i] == last[i]:
19+
i += 1
20+
return first[:i]
1721

1822

1923
def find_common_prefix(left: str, right: str) -> str:
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
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()
6-
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)
112

12-
13-
def is_upper_case(letter: str) -> bool:
14-
return letter == letter.upper()
3+
lowers = set(ch for ch in s if ch.islower())
4+
uppers = set(ch for ch in s if ch.isupper())
5+
count = 0
6+
for ch in uppers:
7+
if ch.lower() not in lowers:
8+
count += 1
9+
return count

0 commit comments

Comments
 (0)