I am having some trouble writing a variant of sub-string search. Essentially the goal is to write a method that can perform sub-string search except that the source data is in an array of Strings rather the one String.
I have looked around and can't find anyone who has managed to solve this elegantly.
Consider some input data such as:
final List<String> source = new ArrayList<String>();
source.add("abc");
source.add("def");
source.add("ghi");
source.add("jkl");
source.add("mnop");
Now let's say I want to write a method that can return a Pair of the first location of where the target String appears. This Pair represents the first index of the String in the source array where the target appears and its index within that String where the target starts.
Examples with 0 based indices:
subStringArray(source, "def"); //returns Pair(1,0) - 2nd string - 1st index
subStringArray(source, "ef"); //returns Pair(1,1) - 2nd string - 2nd index
subStringArray(source, "fgh"); //returns Pair(1,2) - 2nd string - 3rd index
subStringArray(source, "hijklmno"); //returns Pair(2, 1) - 3rd string - 2nd index
subStringArray(source, "abcf"); //returns null or Pair(-1,-1);
I know it would involve three for loops but I'm not sure how to handle the edge cases, i.e where the target String takes up multiple Strings in the source array.
I should note that I can't allocate more memory.