、問題はそれがだかどうかを確認するために各半分をチェックし、ちょうど2つに単語を分割するすべての可能な方法を考慮することによって解決することができます有効な単語。入力文字列の長さがnの場合、文字列を分割する方法はO(n)のみです。高速検索をサポートする構造体に文字列を格納する場合(トライ、ハッシュテーブルなど)
もっと興味深いのは、k> 2ワードで単語を分割する場合です。
単語は、単語に分割してk - 1語に分割することができれば、k個の単語に分割することができます。
再帰的な基本的なケースは、単語がゼロの単語に分割できるのは、それが空の文字列である場合に限られます。
この再帰的な洞察を使用するには、可能なすべての単語の分割を2つの部分に分けて元のアルゴリズムを修正します。分割が完了すると、分割の最初の部分が単語かどうか、分割の2番目の部分がk - 1語に分割できるかどうかを確認できます。最適化として、可能性のあるすべての分割について再帰的に実行するのではなく、最初の単語が有効であることがわかっている分割のみに再帰的に適用します。ここでは、Javaで書かれたいくつかのサンプルコードがあります:それはk個の異なる部分に文字列のすべての可能なパーティションを生成しようとするため
public static boolean isSplittable(String word, int k, Set<String> dictionary) {
/* Base case: If the string is empty, we can only split into k words and vice-
* versa.
*/
if (word.isEmpty() || k == 0)
return word.isEmpty() && k == 0;
/* Generate all possible non-empty splits of the word into two parts, recursing on
* problems where the first word is known to be valid.
*
* This loop is structured so that we always try pulling off at least one letter
* from the input string so that we don't try splitting the word into k pieces
* of which some are empty.
*/
for (int i = 1; i <= word.length(); ++i) {
String first = word.substring(0, i), last = word.substring(i);
if (dictionary.contains(first) &&
isSplittable(last, k - 1, dictionary)
return true;
}
/* If we're here, then no possible split works in this case and we should signal
* that no solution exists.
*/
return false;
}
}
このコードは、最悪の場合には、(nはK)時間Oで実行されます。もちろん、ほとんどの可能な分割がすべての単語を形成することはないので、この最悪の場合の動作には当てはまりそうにありません。
重複している可能性があります[長い文字列の中の単語を検索してください。自動トークン。](http://stackoverflow.com/questions/3901266/find-the-words-in-a-long-stream-of-characters-auto-tokenize) –