Skip to content

Commit 9ca8efe

Browse files
committed
common prefix, count letters
1 parent 96cb1b1 commit 9ca8efe

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

Sprint-2/improve_with_precomputing/common_prefix/common_prefix.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@ 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+
sorted_strings = sorted(strings)
14+
1015
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+
17+
for i in range(len(sorted_strings) - 1):
18+
common = common_prefix(sorted_strings[i], sorted_strings[i + 1])
19+
if len(common) > len(longest):
20+
longest = common
1621
return longest
1722

1823

19-
def find_common_prefix(left: str, right: str) -> str:
24+
def common_prefix(left: str, right: str) -> str:
2025
min_length = min(len(left), len(right))
2126
for i in range(min_length):
2227
if left[i] != right[i]:

Sprint-2/improve_with_precomputing/count_letters/count_letters.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ 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.
44
"""
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)
5+
uppers = set()
6+
lowers = set()
117

8+
for ch in s:
9+
if ch.isupper():
10+
uppers.add(ch)
11+
elif ch.islower():
12+
lowers.add(ch)
1213

13-
def is_upper_case(letter: str) -> bool:
14-
return letter == letter.upper()
14+
count = 0
15+
for i in uppers:
16+
if i.lower() not in lowers:
17+
count += 1
18+
return count

0 commit comments

Comments
 (0)