Path to becoming an Algorithm Guru — #1: Multiple String Matching Problem

Photo by Patrick Tomasso on Unsplash

The data structure that is often used for string matching is Trie. The problem is as follows.

Write a function that takes in a long string and an array of short strings, all of which has a smaller length than the long string. The function should return an array of booleans where each boolean represents if each string of the arrays of short strings is contained in the long string.

The challenge is to not use any built-in string searching methods such as .find in python.

The first solution that comes to mind is to iterate through the arrays of short strings and compare it letter by letter to the long string.

The solution first iterates through the array of short strings.
For each short string, it iterates through the long string and checks if the current letter of the long string is equal to the current letter of the short string.
If it does, the function isInLongString returns True.
If it does not, the search starts again from the next letter of the long string and the first letter of the short string.
It checks if the letters are equal and the process continues for every short string.

The time and space complexity are rather straightforward. The space complexity is O(n) space where n is the number of short strings in the array of short strings.
The time complexity is O(mno) where m is the length of the long string, n is the number of short strings in the array of short strings and o is the length of the longest string in the array of short strings.

The time complexity is O(mno) as for every short string, we are iterating through the long string. And for every iteration, we are at most going to be iterating through the length of the longest string in the array of short strings if it is found in the long string.

A second solution would be to build a suffix tree that contains every suffix of the long string. A long string of “string” would thus have a suffix tree starting from a root node pointing to the following nodes…

g →*, where * represent the end symbol
n →g →*
i →n →g →*
r →i →n →g →*, and so on until …
s →t →r →i →n →g →*

Then, iterating through the array of short strings, we can find a match by simply checking if the first letter is in the current node of the tree and traversing down the trie if it exists.

What is the time and space complexity for this solution?

We need to build a suffix tree that will take O(m²) where m is the length of the long string. We are iterating through every position of the long string and inserting the substring from that position till the end of the string. Once that is done, we need to iterate through the array of short strings and check if it is present in the trie. This would take O(no) time where n is the number of strings in the array of short strings and o is the length of the longest string in the array.
The resulting time complexity would be O(m² + no). The space complexity would be O(m² + n). The m² comes from the trie as we are storing every suffix of the string.

The last solution would be to build a trie from the array of short strings. Then iterating through the long string and looking up the trie at every iteration to see if there are any matches.

The time and space complexity for this solution would be O(no + mo) where n is the number of short strings in the array of short strings, o is the length of the longest string in the array of short strings and m is the length of the long string.
The O(no) comes from the building of the trie from the array of short strings. The O(mo) comes from iterating through the long string and at every iteration, we check if there are matches in the trie.
The space complexity would be O(no + n) which simplifies to O(no).

Which solution is the better one if we are only concerned about time complexity? Comparing the third solution, O(mo + no) and the second solution, O(m² + no). We know that m² must be greater than mo since o must be smaller than m. The length of each string in the array of short strings must be smaller than the long string as specified in the problem. The third solution is therefore faster than the second solution.

Then comparing the first solution, O(mno) and the third solution,
O(mo + no). For the majority of large inputs, it is likely that mo + no is smaller than mno. Since o*(m*n) is likely to be larger than o*(m + n), the third solution is the best in terms of time complexity.

Thank you for reading. Let me know if you spot any mistakes.

Opinions are my own.