Skip to content

Commit 4de72bb

Browse files
committed
implemented precommuting
1 parent cf9e042 commit 4de72bb

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
from typing import List
22

3-
4-
def find_longest_common_prefix(strings: List[str]):
3+
def find_longest_common_prefix(strings: List[str]) -> str:
54
"""
6-
find_longest_common_prefix returns the longest string common at the start of any two strings in the passed list.
7-
8-
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.
5+
find_longest_common_prefix returns the longest string common at the start
6+
of any two strings in the passed list.
97
"""
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
17-
8+
if len(strings) < 2:
9+
return ""
10+
11+
sorted_strings = sorted(strings)
12+
13+
return find_common_prefix(sorted_strings[0], sorted_strings[-1])
1814

1915
def find_common_prefix(left: str, right: str) -> str:
2016
min_length = min(len(left), len(right))
2117
for i in range(min_length):
2218
if left[i] != right[i]:
2319
return left[:i]
24-
return left[:min_length]
20+
return left[:min_length]
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
def count_letters(s: str) -> int:
22
"""
3-
count_letters returns the number of letters which only occur in upper case in the passed string.
3+
count_letters returns the number of letters which only occur in upper case
4+
in the passed string.
45
"""
6+
7+
lowercase_set = set(letter for letter in s if letter == letter.lower())
8+
59
only_upper = set()
610
for letter in s:
711
if is_upper_case(letter):
8-
if letter.lower() not in s:
12+
if letter.lower() not in lowercase_set: # O(1) lookup instead of O(n)
913
only_upper.add(letter)
1014
return len(only_upper)
1115

12-
1316
def is_upper_case(letter: str) -> bool:
14-
return letter == letter.upper()
17+
return letter == letter.upper()

0 commit comments

Comments
 (0)