LeetCode Solutions

745. Prefix and Suffix Search

Time: $O(|\texttt{words}||\texttt{word[i]}|^3) + O(q|\texttt{word[i]}|)$

Space: $O(|\texttt{words}||\texttt{word[i]}|^3)$

			

class WordFilter {
 public:
  WordFilter(vector<string>& words) {
    for (int i = 0; i < words.size(); ++i) {
      const string& word = words[i];
      vector<string> prefixes;
      vector<string> suffixes;
      for (int j = 0; j <= word.length(); ++j) {
        const string prefix = word.substr(0, j);
        const string suffix = word.substr(j);
        prefixes.push_back(prefix);
        suffixes.push_back(suffix);
      }
      for (const string& prefix : prefixes)
        for (const string& suffix : suffixes)
          keyToIndex[prefix + '_' + suffix] = i;
    }
  }

  int f(string prefix, string suffix) {
    const string key = prefix + '_' + suffix;
    if (keyToIndex.count(key))
      return keyToIndex[key];
    return -1;
  }

 private:
  unordered_map<string, int> keyToIndex;
};
			

class WordFilter {
  public WordFilter(String[] words) {
    for (int i = 0; i < words.length; ++i) {
      final String word = words[i];
      List<String> prefixes = new ArrayList<>();
      List<String> suffixes = new ArrayList<>();
      for (int j = 0; j <= word.length(); ++j) {
        final String prefix = word.substring(0, j);
        final String suffix = word.substring(j);
        prefixes.add(prefix);
        suffixes.add(suffix);
      }
      for (final String prefix : prefixes)
        for (final String suffix : suffixes)
          keyToIndex.put(prefix + '_' + suffix, i);
    }
  }

  public int f(String prefix, String suffix) {
    final String key = prefix + '_' + suffix;
    if (keyToIndex.containsKey(key))
      return keyToIndex.get(key);
    return -1;
  }

  private Map<String, Integer> keyToIndex = new HashMap<>();
}
			

struct TrieNode {
  vector<shared_ptr<TrieNode>> children;
  int weight = -1;
  TrieNode() : children(27) {}
};

class Trie {
 public:
  void insert(const string& word, int weight) {
    shared_ptr<TrieNode> node = root;
    for (const char c : word) {
      const int i = c - 'a';
      if (node->children[i] == nullptr)
        node->children[i] = make_shared<TrieNode>();
      node = node->children[i];
      node->weight = weight;
    }
  }

  int startsWith(const string& word) {
    shared_ptr<TrieNode> node = root;
    for (const char c : word) {
      const int i = c - 'a';
      if (node->children[i] == nullptr)
        return -1;
      node = node->children[i];
    }
    return node->weight;
  }

 private:
  shared_ptr<TrieNode> root = make_shared<TrieNode>();
};

class WordFilter {
 public:
  WordFilter(vector<string>& words) {
    for (int i = 0; i < words.size(); ++i)
      for (int j = 0; j <= words[i].length(); ++j)
        trie.insert(words[i].substr(j) + '{' + words[i], i);
  }

  int f(string prefix, string suffix) {
    return trie.startsWith(suffix + '{' + prefix);
  }

 private:
  Trie trie;
};