Prefix Count: Hashtable
Usually there are two approaches to deal with prefixes (or suffixes) in strings: either using a Trie, or a Hashtable. The former saves space while the latter consumes more but it is an easier implementation. Both have the relative save time complexity. For this problem below, given the small constraints, I decided to go with a Hashtable. No caveats, just follow the instructions given in the problem description. Cheers, ACC. Number of Prefix Connected Groups - LeetCode You are given an array of strings words and an integer k . Two words a and b at distinct indices are prefix -connected if a[0..k-1] == b[0..k-1] . A connected group is a set of words such that each pair of words is prefix-connected. Return the number of connected groups that contain at least two words, formed from the given words. Note: Words with length less than k cannot join any group and are ignored. ...