This is the next question of this answer and the pseudo-code algorithm that was published by the user. I did not comment on this question because of his age. I'm only interested in checking whether it is possible to split the string into words. The algorithm should not actually split the string. This is the answer to a related question:
Let S [1..length (w)] be a table with Boolean elements. S [i] is true if the word w [1..i] can be broken. Then set S [1] = isWord (w [1]) and for i = 2 to the length (w) calculate
S [i] = (isWord [w [1..i] or for any j from {2..i}: S [j-1] and isWord [j..i]).
I translated this algorithm into simple python code, but I'm not sure if I understand it correctly. The code:
def is_all_words(a_string, dictionary)):
str_len = len(a_string)
S = [False] * str_len
S[0] = is_word(a_string[0], dictionary)
for i in range(1, str_len):
check = is_word(a_string[0:i], dictionary)
if (check):
S[i] = check
else:
for j in range(1, str_len):
check = (S[j - 1] and is_word(a_string[j:i]), dictionary)
if (check):
S[i] == True
break
return S
. 1) Python, , 2) , S, , , ? is_word - , . .
: , , . :
def is_all_words(a_string, dictionary)):
str_len = len(a_string)
S = [False] * str_len
S[0] = is_word(a_string[0], dictionary)
for i in range(1, str_len):
check = is_word(a_string[0:i], dictionary)
if (check):
S[i] = check
else:
for j in range(1, i):
check = (S[j - 1] and is_word(a_string[j:i]), dictionary)
if (check):
S[i] == True
break
return S
a_string = "carrotforever"
S = is_all_words(a_string, dictionary)
print(S[len(S) - 1])
a_string = "hello"
S = is_all_words(a_string, dictionary)
print(S[len(S) - 1])
True .