Given a string such :
var str = "thisisinsane";
assisted by a list of words from a dictionary such:
var dic = [ "insane", "i", "is", "sin", "in", "this", "totally" ];
How to split str
into words?
For this string, there are 3 words to identify. But we need avoid the pitfalls. To avoid them most of the time, I know we can attack the sentence by the left, and try to find the longest word prossible. When found, we could attack the rest of the string, etc.
Below : the input, the possible pitfalls, and the wanted output in bottom right.
thisisinsane
|
|
(this)isinsane
/ \
/ \
(this,i)sinsane (this,is)insane
/ / \
/ / \
(this,i,sin)ane (this,is,in)sane (this,is,insane)
/ <BEST IS>
/ <THIS ONE>
(this,is,in,sane)
At the end, we want to get :
var splited = ["this", "is", "insane"];