文字列(userInputと呼ばれます)と一致させる最良の方法を見つけようとしています。この文字列は他のいくつかの連結の結果です文字列(がを承認したとの例では、私が呼ばれるのArrayListまたは配列)arraylistの文字列がarraylistからの連結の結果であるところ
ArrayList<String> approved = new ArrayList<>();
approved.add("abc");
approved.add("def");
approved.add("ghi");
approved.add("def jkl ggwp my life"); //repeated elements (abc,jkl)
approved.add("jkl");
approved.add("mno");
approved.add("pqr");
approved.add("stu vwx");
approved.add("yz");
私はちょうど私が持っている、現実の世界では、私のdifficulty.Butを説明するために(上記の)このArrayListのを使用します
-fixed arraylist which wont have dynamic elements (the elements in the arraylist wont change)
-arraylist with more than 6000 elements
-elements in the arraylist contains multiple word e.g ("stu vwx")
-repeated elements but concatenated with another string in the arraylist
以下は、
USERINPUT
userInput = "yza"; //obviously it doesnt match anything
userInput = "ghi jk"; //doesnt match the concatenated string (ghi and jkl)
userInput = "pqr stu v"; //it can split the element with whitespace,but it has to take the whole word
userInput = "def abc"; //the order are important
イムであればプログラムは次はUSERINPUTある場合はtrueを返し
userInput = "abc def";
userInput = "stu vwx yz"; //they can combine with another element as a whole
userInput = "ghi"; //it doesnt have to combine with another element
userInput = "vwx yz"; //they can split the arraylist elements with whitespace only and concatenate it with another element
しかし、プログラムが偽を返すもの
ユーザー入力を分割すると考えてfirstWordとlastWordですので、にはが含まれており、arraylist内のインデックスを見つけることができます。
firstWordとfirstWord以来lastWordを(そのジョブを実行し、文字列を持っている要素の複数のインデックスを返します)(.contains、のはここから
String userInput = "def ghi jkl mno";
//so,the firstWord and lastWord will be
firstWord = "def";
lastWord = "mno";
を言うをしてみましょうarrylistでが複数回出現した場合)、別の配列で可能な一致を返すように設定されます。
firstWordPossibleIndex[] = {1,4};
lastWordPossibleIndex[] = {6};
順序が重要であるので、ここでのロジックはfirstWordPossibleIndexがlastWordPossibleIndexよりも低い値が含まれている必要があり、そう、任意の大きな値があるかどう提供する文字列があることになるので、それを除去することができるということです無効。
そのロジックを実装した後、それはこの場合には意味lastWordPossibleIndex
にfirstWordPossibleIndexから次のインデックスをマッチング開始する必要があり、それは、USERINPUTで2番目の単語をチェックし、一致しようとするだろう2及び5のインデックスを持つ要素とそれがlastwordPossibleIndex までチェックする
(のでfirstWordPossibleIndex 1および4です)と、すべての単語がarraylistに従って順番に並んでいる場合は、trueを返します。
これで、私はまだの文字列と一致する文字列と一致しています。と別の文字列の一部が一致しています。これを解決する考えはありますか?
これを解決するライブラリがありますか?