gpt4 book ai didi

java - 匹配 arraylist 中的字符串,其中字符串是 arraylist 串联的结果

转载 作者:太空宇宙 更新时间:2023-11-04 11:19:19 25 4
gpt4 key购买 nike

我试图找出匹配字符串(称为userInput)的最佳方法,其中该字符串是其他几个字符串(数组列表或数组,在示例中我称为approved)串联的结果

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");

我将使用这个数组列表(上面)来解释我的困难。但是,在现实世界中,我有一个

    -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

如果以下是用户输入,程序应返回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

但是,如果以下是用户输入,程序将返回 false

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以获取firstWordlastWord,因为顺序很重要。然后,使用contains在数组列表中查找它们的索引。

比方说

String userInput = "def ghi jkl mno";
//so,the firstWord and lastWord will be
firstWord = "def";
lastWord = "mno";

从这里开始,.contains()将完成其工作并返回具有字符串firstWordlastWord的元素的多个索引(因为firstWord在数组列表中出现多次),它将被配置为返回另一个数组中可能的匹配项。

firstWordPossibleIndex[] = {1,4};
lastWordPossibleIndex[] = {6};

由于顺序很重要,因此这里的逻辑是 firstWordPossibleIndex 应该包含比 lastWordPossibleIndex 更低的值,因此,如果有任何更大的值,可以将其删除,因为提供的字符串无效。

实现该逻辑后,它应该开始匹配从 firstWordPossibleIndexlastWordPossibleIndex 的下一个索引

在本例中,它将检查 userInput 中的第二个单词,并尝试与索引为 2 和 5 的元素进行匹配(因为 firstWordPossibleIndex 为 1 和 4)

它会检查lastwordPossibleIndex,如果所有单词都按照数组列表排序,则返回 true。

有了这个,我仍然无法将连接的字符串与另一个字符串的一部分进行匹配。你有什么办法解决这个问题吗?

有没有库可以解决这个问题?

最佳答案

你可以尝试这样的事情:

import java.util.ArrayList;
import java.util.List;

public class WhatSoEver{

static int order;

public static void main(String[] args) {

ArrayList<String> approved = new ArrayList<>();
approved.add("abc");
approved.add("def");
approved.add("ghi");
approved.add("def jkl ggwp my life");
approved.add("jkl");
approved.add("mno");
approved.add("pqr");
approved.add("stu vwx");
approved.add("yz");

System.out.println(isValid(approved, "abc def")); // true
System.out.println(isValid(approved, "stu vwx yz")); // true
System.out.println(isValid(approved, "ghi")); // true
System.out.println(isValid(approved, "vwx yz")); // true

System.out.println(isValid(approved, "yza")); // false
System.out.println(isValid(approved, "ghi jk")); //false
System.out.println(isValid(approved, "pqr stu v")); //false
System.out.println(isValid(approved, "def abc")); //false

}

public static boolean isValid(List<String> approved, String userInput){
order=0;
for(String word : userInput.split(" ")){
if(!containsWord(approved, word)){
return false;
}
}
return true;
}

private static boolean containsWord(List<String> approved, String word){
for(int i=0; i<approved.size(); i++){
for(String subS : approved.get(i).split(" ")){
if(word.equals(subS) && (i+1)>order){
order=i;
return true;
}
}
}
return false;
}
}

输出

true
true
true
true
false
false
false
false

关于java - 匹配 arraylist 中的字符串,其中字符串是 arraylist 串联的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45105813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com