Press "Enter" to skip to content

Posts tagged as “prefix”

花花酱 LeetCode 14. Longest Common Prefix

Problem

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

Solution: Brute Force

Time complexity: O(mk), where k the length of common prefix.

Space complexity: O(k)

C++

Java

Time complexity: (mk + k^2)

Python3

 

花花酱 LeetCode 745. Prefix and Suffix Search

Link: https://leetcode.com/problems/prefix-and-suffix-search/description/

Problem:

Given many wordswords[i] has weight i.

Design a class WordFilter that supports one function, WordFilter.f(String prefix, String suffix). It will return the word with given prefix and suffix with maximum weight. If no word exists, return -1.

Examples:

Note:

  1. words has length in range [1, 15000].
  2. For each test case, up to words.length queries WordFilter.f may be made.
  3. words[i] has length in range [1, 10].
  4. prefix, suffix have lengths in range [0, 10].
  5. words[i] and prefix, suffix queries consist of lowercase letters only.

Idea:

Construct all possible filters

 

Solution1:

C++

Time complexity: O(NL^3 + QL)  where N is the number of words, L is the max length of the word, Q is the number of queries.

Space complexity: O(NL^3)

Version #2

Solution 2:

C++ / Trie

Time complexity: O(NL^2 + QL)  where N is the number of words, L is the max length of the word, Q is the number of queries.

Space complexity: O(NL^2)

Related Problems:

花花酱 LeetCode 720. Longest Word in Dictionary

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.

If there is no answer, return the empty string.

Example 1:

Example 2:

Note:

 

  • All the strings in the input will only contain lowercase letters.
  • The length of words will be in the range [1, 1000].
  • The length of words[i] will be in the range [1, 30].

Idea:

Brute force

Trie

Solution:

C++

 

 

Trie + Sorting

 

Trie + No sorting

 

花花酱 LeetCode 208. Implement Trie (Prefix Tree)

Problem:

Implement a trie with insertsearch, and startsWith methods.

Note:
You may assume that all inputs are consist of lowercase letters a-z.

Idea:




Tree/children array

 

 

Solution:

C++ / Array

 

C++ / hashmap

 

Java

 

Python 1:

 

Python 2:

 

花花酱 LeetCode 677. Map Sum Pairs

https://leetcode.com/problems/map-sum-pairs/description/

Problem:

Implement a MapSum class with insert, and sum methods.

For the method insert, you’ll be given a pair of (string, integer). The string represents the key and the integer represents the value. If the key already existed, then the original key-value pair will be overridden to the new one.

For the method sum, you’ll be given a string representing the prefix, and you need to return the sum of all the pairs’ value whose key starts with the prefix.

Example 1:

Idea:

Prefix tree



Solution 1

Solution 2:

with std::unique_ptr