I am trying to get the count of substrings present in a list of million words in java. Looping through each of these strings to check if the previous value contains the next value seems to have a major performance issue. With fewer words, it works fine but when a huge list of say a million words is involved, it takes ages to give the count back. Can someone tell me the fastest approach to this?