aboutsummaryrefslogtreecommitdiff
path: root/challenge-265/lubos-kolouch/python/ch-2.py
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2024-04-20 15:38:13 +0200
committerLubos Kolouch <lubos@kolouch.net>2024-04-20 15:38:13 +0200
commitdc33da454b62dad3a43f4277b477892774ed82b5 (patch)
tree3964371a9e940707725218121f16f2370c765bc4 /challenge-265/lubos-kolouch/python/ch-2.py
parentcdf8415b097af3c94b7e5f879f810a03628d8493 (diff)
downloadperlweeklychallenge-club-dc33da454b62dad3a43f4277b477892774ed82b5.tar.gz
perlweeklychallenge-club-dc33da454b62dad3a43f4277b477892774ed82b5.tar.bz2
perlweeklychallenge-club-dc33da454b62dad3a43f4277b477892774ed82b5.zip
Challenge 265 LK Perl Python
Diffstat (limited to 'challenge-265/lubos-kolouch/python/ch-2.py')
-rw-r--r--challenge-265/lubos-kolouch/python/ch-2.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/challenge-265/lubos-kolouch/python/ch-2.py b/challenge-265/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..98270f0f99
--- /dev/null
+++ b/challenge-265/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,20 @@
+from collections import Counter
+from typing import List
+
+
+def shortest_completing_word(target: str, words: List[str]) -> str:
+ # Function to clean and count the letters in a word
+ def get_letter_counts(s: str) -> Counter:
+ return Counter(c.lower() for c in s if c.isalpha())
+
+ target_count = get_letter_counts(target)
+
+ # Filter and find the shortest completing word
+ valid_words = [word for word in words if not target_count - get_letter_counts(word)]
+ return min(valid_words, key=len) if valid_words else ""
+
+
+# Test cases
+assert shortest_completing_word("aBc 11c", ["accbbb", "abc", "abbc"]) == "accbbb"
+assert shortest_completing_word("Da2 abc", ["abcm", "baacd", "abaadc"]) == "baacd"
+assert shortest_completing_word("JB 007", ["jj", "bb", "bjb"]) == "bjb"