How would you handle time and space complexity when writing code for a matching or parsing problem on Alignerr?
Implement find_matches(text, patterns) to return every occurrence of each pattern in text, including overlapping matches. Return matches as [start, end, pattern], where end is exclusive, ordered by start index and then match end. For example, text = "aaaa", patterns = ["aa", "aaa"] returns all five matches shown by those rules.
Constraints: 1 <= len(text) <= 5000, patterns are distinct non-empty lowercase strings, and their total length is at most 5000.
def find_matches(text, patterns):