If each line is given by a list of words, these are actually two sorted sublists:
(1) odd word list: sorted by length by length
(2) even-word list: sorted by length
Use binary search in both lists with a comparator: word.length()
As soon as you find a match [the word you are looking for and the word in the list that you are currently looking at) is the same length: check if it is the same word.
repeated for each row.
Complexity [for each line]: O(logn * |S|) where |S| is the size of your word, and n is the number of words per line.
source share