File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Matching 2 arrays of strings Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Matching 2 arrays of strings" Watch "Matching 2 arrays of strings" New topic
Author

Matching 2 arrays of strings

Ram Viswanathan
Greenhorn

Joined: Dec 08, 2008
Posts: 2
Hi

I have 2 sets of string arrays, say for example
array 1 -> "str1", "str2", "str3", "str12", "str13",....
array 2 -> "s", "t"

I am trying to do a "like search" of 2 on 1. This means that for every element in the array #2, I would find all matching strings from array #1. I end up writing 2 for loops as shown below

for (i from 0 to length of array 1) {
for (j from 0 to length of array 2) {
//check if ith element of array 1 contains jth element of array 2

}
}

If my array 1 and array 2 are really large arrays, then i end up doing a lot of looping. Can this be optimized?

Thanks
Ram
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32830
    
    4
Welcome to JavaRanch

I can't think of how to optimise your searches; you have got two parallel linear searches, no three parallel linear searches, if I have understood the question correctly:
  • Through the individual Strings of array 1.
  • Through the individual Strings of array 2.
  • Through the length of the current String to see whether part of it matches.
  • So you are stuck with cubic complexity.
    It is worthwhile seeing whether it is possible to make one of the arrays into a regular expression, then iterating through the other array looking for matches, but I don't know whether that is any faster.
    Dawn Charangat
    Ranch Hand

    Joined: Apr 26, 2007
    Posts: 249
    There is one way, as I think... but could involve a bit of preparation work on those arrays before the search operation.

    step1: sort both the arrays [ascending or descending, but make sure both of the arrays are in the same order].
    step2: start searching in order, and break the moment if an element in array2 is not found in array1.

    As I mentioned, there is an added overhead of sorting the arrays before the operation, but if you can handle that, then this could optimize your search.

    Dawn.
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 35437
        
        9
    The possible approaches depend on how you define "matching strings". By String.equals == true? By String.startsWith == true? By String.indexOf != -1?


    Android appsImageJ pluginsJava web charts
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: Matching 2 arrays of strings
     
    Similar Threads
    sorting multidimensional arrays (int)
    MultiDimenaional Array sort
    Quick String to <char> Array Conversion
    Recursion - function call trees
    Ajax Suggest