aboutsummaryrefslogtreecommitdiff
path: root/challenge-265/lubos-kolouch/python/ch-2.py
diff options
context:
space:
mode:
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"