Skip to content

Commit 524d680

Browse files
committed
task is done
1 parent f13588d commit 524d680

2 files changed

Lines changed: 31 additions & 25 deletions

File tree

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1+
12
from typing import List
23

34

4-
def find_longest_common_prefix(strings: List[str]):
5+
def find_longest_common_prefix(strings: List[str]) -> str:
56
"""
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.
7+
Return the longest prefix shared by any two strings.
8+
Return an empty string if fewer than two strings are provided.
99
"""
10+
if len(strings) < 2:
11+
return ""
12+
13+
prefix_counts = {}
14+
15+
for string in strings:
16+
for i in range(1, len(string) + 1):
17+
prefix = string[:i]
18+
prefix_counts[prefix] = prefix_counts.get(prefix, 0) + 1
19+
1020
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
1721

22+
for prefix, count in prefix_counts.items():
23+
if count >= 2 and len(prefix) > len(longest):
24+
longest = prefix
1825

19-
def find_common_prefix(left: str, right: str) -> str:
20-
min_length = min(len(left), len(right))
21-
for i in range(min_length):
22-
if left[i] != right[i]:
23-
return left[:i]
24-
return left[:min_length]
26+
return longest
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
12
def count_letters(s: str) -> int:
23
"""
3-
count_letters returns the number of letters which only occur in upper case in the passed string.
4+
Count uppercase letters that never appear in lowercase.
45
"""
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)
6+
lowercase_letters = set()
7+
uppercase_letters = set()
118

9+
for letter in s:
10+
if letter.islower():
11+
lowercase_letters.add(letter)
12+
elif letter.isupper():
13+
uppercase_letters.add(letter)
1214

13-
def is_upper_case(letter: str) -> bool:
14-
return letter == letter.upper()
15+
return sum(
16+
1 for letter in uppercase_letters
17+
if letter.lower() not in lowercase_letters
18+
)

0 commit comments

Comments
 (0)