-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcan_be_build_from_dict.py
More file actions
38 lines (32 loc) · 1.31 KB
/
can_be_build_from_dict.py
File metadata and controls
38 lines (32 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Validate if you can form the string based on a given dictionary
#
# "catfoxdog" ("cat", "dog" "cow", "foxdog") => True
# "catfoxdog" ("cat", "dog" "cow") => False
# "catfoxcat" ("cat", "dog" "cow") => False
# "" ("cat", "dog" "cow") => True
# "catfoxdog" ("catfox", "dog", "cat") => False
# "catfoxdog" ("catfox", "foxdog", "cat") => True
# "catfoxcat" () => False // dict will have at least 1 valid word
# "catsdog" ("cat", "dog") => False // dict will have at least 1 valid word
# "catsdog" ("cat", "dog" "cats") => True // dict will have at least 1 valid word
def find_words(target, dictionary):
# target: str
# dictionary: list
if target == "":
return True
for word in dictionary:
if target.startswith(word):
# target catfoxdog 0-3-1 = 0:2=
# word cat => 3
if find_words(target[len(word):], dictionary) == True:
return True
return False
print(find_words("catfoxdog", ["cat", "dog", "cow", "foxdog"]))
print(find_words("catfoxdog", ["cat", "dog", "cow"]))
print(find_words("catfoxcat", ("cat", "dog", "cow")))
print(find_words("", ("cat", "dog", "cow")))
print(find_words("catfoxdog", ["catfox", "dog", "cat"]))
print(find_words("catfoxdog", ("catfox", "foxdog", "cat")))
print(find_words("catfoxcat", ()))
print(find_words("catsdog", ("cat", "dog")))
print(find_words("catsdog", ("cat", "dog", "cats")))