0
The most efficient way to solve this problem.
You have an Array with words entered by the User. So you don't know which words this Array could contain. Now you want to filter and delete which words appear in others words. An Example: Array: {appleecho, supermario, echomail} As you can see "echo" appears in the first and the third word. My Problem: I can't use appleecho.contains(echomail) because Java won't find "echo". And I can't use appleecho.contain("echo") because it could be another word that I'm searching for. I would compare the letters of the words but this would end in many loops because I also need to get every word plus every other word plus the letter comparison. How would you do this and whats an efficient way to do it. Sorry for my bad englisch.
5 odpowiedzi
+ 6
import java.util.ArrayList;
public class Program
{
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList();
arr.add("appleecho");
arr.add("pineapple");
arr.add("echoapple");
for(String s : arr) {
System.out.println(s);
}
System.out.println("\nFiltered results");
for(String s : arr) {
if(s.contains("echo")) {
System.out.println(s);
}
}
}
}
+ 3
snake3444
https://code.sololearn.com/cyWlXhUy8P36/?ref=app
Just an idea. The code is maybe not bug free and you need to find the longest substring in the arraylist.
Edit: I added another example:
"hellomyworld"
"hellothisworld"
--> inside the list you find hello and world
+ 3
About StringBuffer.substring()
https://www.tutorialspoint.com/java/lang/stringbuffer_substring_end.htm
0
Can't you just store the string in a variable and use that?
string s="echo" ;
... contains(s)...
0
No because it could be any other word that iam searching for.
It could also be: {jesusapple, woods, applepie}.
Then the Program should filter the "apple".
The User decides which words the array contains.