Problem: Given a vector of strings, find the number of groups of strings where each group is all related to a prefix which exists in the input. For example, ["abc", "abd"] should return 2. ["ab", "abc", "abd"] should return 1. ["abc", "abcd", "xyz", "xyza"] should return 2.
I was told this problem can be solved in O(n) using Union-Find, but I can't figure it out. Any ideas for the Union-Find solution? I know how to do it using sorting and Tries.
Also, is there an equivalent to this problem on leetcode? I did not find any.