gpt4 book ai didi

java - 查找包含数组中所有单词的字符串的子字符串

转载 作者:行者123 更新时间:2023-11-30 11:43:37 25 4
gpt4 key购买 nike

我有一个字符串和一个单词数组,我必须编写代码来查找包含数组中所有单词的字符串的所有子字符串,顺序不限。该字符串不包含任何特殊字符/数字,每个单词由空格分隔。

例如:

给定的字符串:

aaaa aaaa aaaa aaaa cccc bbbb bbbb bbbb bbbb aaaa bbbb cccc

数组中的单词:

aaaa
bbbb
cccc

输出示例:

aaaa aaaa aaaa aaaa cccc bbbb bbbb bbbb bbbb    

aaaa aaaa aaaa aaaa cccc bbbb

aaaa cccc bbbb bbbb bbbb bbbb

cccc bbbb bbbb bbbb bbbb aaaa

aaaa cccc bbbb

我已经使用 for 循环实现了这个,但是效率很低。

我怎样才能更有效地做到这一点?

我的代码:

    for(int i=0;i<str_arr.length;i++)
{
if( (str_arr.length - i) >= words.length)
{
String res = check(i);
if(!res.equals(""))
{
System.out.println(res);
System.out.println("");
}
reset_all();
}
else
{
break;
}
}

public static String check(int i)
{
String res = "";
num_words = 0;

for(int j=i;j<str_arr.length;j++)
{
if(has_word(str_arr[j]))
{
t.put(str_arr[j].toLowerCase(), 1);
h.put(str_arr[j].toLowerCase(), 1);

res = res + str_arr[j]; //+ " ";

if(all_complete())
{
return res;
}

res = res + " ";
}
else
{
res = res + str_arr[j] + " ";
}

}
res = "";
return res;
}

最佳答案

我的第一种方法类似于以下伪代码

  for word:string {
if word in array {
for each stored potential substring {
if word wasnt already found {
remove word from notAlreadyFoundList
if notAlreadyFoundList is empty {
use starting pos and ending pos to save our substring
}
}
store position and array-word as potential substring
}

这应该有不错的性能,因为您只遍历字符串一次。

[编辑]

这是我的伪代码的一个实现,试一试,看看它的性能是好是坏。它在假设找到最后一个单词后立即找到匹配的子字符串的情况下工作。如果您确实想要所有 匹配项,请更改标记为//ALLMATCHES 的行:

class SubStringFinder {
String textString = "aaaa aaaa aaaa aaaa cccc bbbb bbbb bbbb bbbb aaaa bbbb cccc";
Set<String> words = new HashSet<String>(Arrays.asList("aaaa", "bbbb", "cccc"));

public static void main(String[] args) {
new SubStringFinder();
}

public SubStringFinder() {
List<PotentialMatch> matches = new ArrayList<PotentialMatch>();
for (String textPart : textString.split(" ")) {
if (words.contains(textPart)) {
for (Iterator<PotentialMatch> matchIterator = matches.iterator(); matchIterator.hasNext();) {
PotentialMatch match = matchIterator.next();
String result = match.tryMatch(textPart);
if (result != null) {
System.out.println("Match found: \"" + result + "\"");
matchIterator.remove(); //ALLMATCHES - remove this line
}
}
Set<String> unfound = new HashSet<String>(words);
unfound.remove(textPart);
matches.add(new PotentialMatch(unfound, textPart));
}// ALLMATCHES add these lines
// else {
// matches.add(new PotentialMatch(new HashSet<String>(words), textPart));
// }
}
}

class PotentialMatch {
Set<String> unfoundWords;
StringBuilder stringPart;
public PotentialMatch(Set<String> unfoundWords, String part) {
this.unfoundWords = unfoundWords;
this.stringPart = new StringBuilder(part);
}
public String tryMatch(String part) {
this.stringPart.append(' ').append(part);
unfoundWords.remove(part);
if (unfoundWords.isEmpty()) {
return this.stringPart.toString();
}
return null;
}
}
}

关于java - 查找包含数组中所有单词的字符串的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11224034/

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