The most efficient way to solve this problem. | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
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.

16th May 2019, 1:47 PM
snake3444
snake3444 - avatar
5 Réponses
+ 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); } } } }
16th May 2019, 4:05 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 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
16th May 2019, 5:49 PM
Denise Roßberg
Denise Roßberg - avatar
16th May 2019, 6:18 PM
Denise Roßberg
Denise Roßberg - avatar
0
Can't you just store the string in a variable and use that? string s="echo" ; ... contains(s)...
16th May 2019, 2:37 PM
HonFu
HonFu - avatar
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.
16th May 2019, 4:37 PM
snake3444
snake3444 - avatar