-1
$\begingroup$

Suppose all the spaces between the words(only small letter alphabets and no punctuation) in a file has been deleted and we need to reconstruct the file. There is a dictionary available which answers whether a word is valid or not in O(1).

Brute force approach is to use backtracking i.e try out all the possibilities. Take for example: "donotsitonthefloor" I start from d, and check if "d" is in dictionary, if not i move ahead. then check for "do", it is a valid word, so i check whether "notsitonthefloor" can be split into valid words. And so on ..

Now coming to analyzing time complexity of this approach, the best i can come up with is:

$T(n) = \sum_{i=1}^n T(n-i) + O(n)$

1) Is the above equation correct? If so, is it possible to simplify it? (i suppose master's theorem can't be applied to this kind of equation).

2) Suppose if dynamic programming is used to store results of repeated calls, i.e. using an array to store whether input[i:n] is valid or not and checking first in this array before making a repeated call. What would be the running time after applying this optimization?

$\endgroup$
2
$\begingroup$

Some remarks of general nature since I think you're operating based on flawed assumptions.

Removing spaces is not unambiguously reversible (unless your dictionary is prefix-free, or restricted in other suitable ways). I'm sure you can easily come up with example strings that can be split into valid English words in several ways.

If your dictionary is prefix-free, a linear scan (e.g. based on a finite automaton) is quite enough, after pre-processing the dictionary. That's basically how lexers work.

In case your dictionary is not restricted and you want to find any (or all) valid split, dynamic programming seems prudent. Before analysing it, write down the algorithm in suitable detail; then use the standard approach.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.