Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially match strings in case of List.contains(String)

Tags:

I have a List<String>

List<String> list = new ArrayList<String>();
list.add("ABCD");
list.add("EFGH");
list.add("IJ KL");
list.add("M NOP");
list.add("UVW X");

if I do list.contains("EFGH"), it returns true. Can I get a true in case of list.contains("IJ")? I mean, can I partially match strings to find if they exist in the list?

I have a list of 15000 strings. And I have to check about 10000 strings if they exist in the list. What could be some other (faster) way to do this?

Thanks.

like image 946
y2p Avatar asked Jul 11 '11 03:07

y2p


People also ask

How do you partially match a string in Python?

Use the in operator for partial matches, i.e., whether one string contains the other string. x in y returns True if x is contained in y ( x is a substring of y ), and False if it is not. If each character of x is contained in y discretely, False is returned.

How do you check if a string contains a substring from a list Java?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do you do a partial match in R?

To do a Partial String Matching in R, use the charmatch() function. The charmatch() function accepts three arguments and returns the integer vector of the same length as input.


2 Answers

If suggestion from Roadrunner-EX does not suffice then, I believe you are looking for Knuth–Morris–Pratt algorithm.

Time complexity:

  • Time complexity of the table algorithm is O(n), preprocessing time
  • Time complexity of the search algorithm is O(k)

So, the complexity of the overall algorithm is O(n + k).

  • n = Size of the List
  • k = length of pattern you are searching for

Normal Brute-Force will have time complexity of O(nm)

Moreover KMP algorithm will take same O(k) complexity for searching with same search string, on the other hand, it will be always O(km) for brute force approach.

like image 83
Kowser Avatar answered Sep 23 '22 14:09

Kowser


Perhaps you want to put each String group into a HashSet, and by fragment, I mean don't add "IJ KL" but rather add "IJ" and "KL" separately. If you need both the list and this search capabilities, you may need to maintain two collections.

like image 23
Hovercraft Full Of Eels Avatar answered Sep 20 '22 14:09

Hovercraft Full Of Eels