Press "Enter" to skip to content

Posts published in “Hashtable”

花花酱 LeetCode 760. Find Anagram Mappings

题ē›®å¤§ę„ļ¼šč¾“å‡ŗꕰē»„Aäø­ęƏäøŖ元ē“ åœØꕰē»„Bäø­ēš„ē“¢å¼•ć€‚

Given two listsĀ AandĀ B, andĀ BĀ is an anagram ofĀ A.Ā BĀ is an anagram ofĀ AĀ meansĀ BĀ is made by randomizing the order of the elements inĀ A.

We want to find anĀ index mappingĀ P, fromĀ AĀ toĀ B. A mappingĀ P[i] = jĀ means theĀ ith element inĀ AĀ appears inĀ BĀ at indexĀ j.

These listsĀ AĀ andĀ BĀ may contain duplicates. If there are multiple answers, output any of them.

For example, given

We should return

asĀ P[0] = 1Ā because theĀ 0th element ofĀ AĀ appears atĀ B[1], andĀ P[1] = 4Ā because theĀ 1st element ofĀ Aappears atĀ B[4], and so on.

Solution:

C++

Time complexity: O(nlogn)

Space complexity: O(n)

C++ / No duplication

 

花花酱 LeetCode 748. Shortest Completing Word

题ē›®å¤§ę„: ē»™ä½ äø€äøŖē”±å­—ęÆå’Œę•°å­—ē»„ęˆč½¦ē‰Œć€‚另外ē»™ä½ äø€äŗ›å•čƍļ¼Œč®©ä½ ę‰¾äø€äøŖ꜀ēŸ­ēš„单čÆčƒ½å¤Ÿč¦†ē›–ä½č½¦ē‰Œäø­ēš„å­—ęƍļ¼ˆäøč€ƒč™‘大小写ļ¼‰ć€‚å¦‚ęžœęœ‰å¤šäøŖč§£ļ¼Œč¾“å‡ŗē¬¬äø€äøŖč§£ć€‚

Problem:

Find the minimum length word from a given dictionaryĀ words, which has all the letters from the stringĀ licensePlate. Such a word is said toĀ completeĀ the given stringĀ licensePlate

Here, for letters we ignore case. For example,Ā "P"Ā on theĀ licensePlateĀ still matchesĀ "p"Ā on the word.

It is guaranteed an answer exists. If there are multiple answers, return the one that occurs first in the array.

The license plate might have the same letter occurring multiple times. For example, given aĀ licensePlateĀ ofĀ "PP", the wordĀ "pair"Ā does not complete theĀ licensePlate, but the wordĀ "supper"Ā does.

Example 1:

Example 2:

Note:

  1. licensePlateĀ will be a string with length in rangeĀ [1, 7].
  2. licensePlateĀ will contain digits, spaces, or letters (uppercase or lowercase).
  3. wordsĀ will have a length in the rangeĀ [10, 1000].
  4. EveryĀ words[i]Ā will consist of lowercase letters, and have length in rangeĀ [1, 15].

Idea:

Hashtable

Solution:

C++

Time complexity: ę—¶é—“å¤ę‚åŗ¦ O(N*26), N is number of words.

Space complexity: ē©ŗé—“å¤ę‚åŗ¦ O(26) = O(1)

 

花花酱 LeetCode 745. Prefix and Suffix Search

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

Problem:

Given manyĀ words,Ā words[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 737. Sentence Similarity II

Problem:

Given two sentencesĀ words1, words2Ā (each represented as an array of strings), and a list of similar word pairsĀ pairs, determine if two sentences are similar.

For example,Ā words1 = ["great", "acting", "skills"]Ā andĀ words2 = ["fine", "drama", "talent"]Ā are similar, if the similar word pairs areĀ pairs = [["great", "good"], ["fine", "good"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relationĀ isĀ transitive. For example, if “great” and “good” are similar, and “fine” and “good” are similar, then “great” and “fine”Ā are similar.

Similarity is also symmetric. For example, “great” and “fine” being similar is the same as “fine” and “great” being similar.

Also, a word is always similar with itself. For example, the sentencesĀ words1 = ["great"], words2 = ["great"], pairs = []Ā are similar, even though there are no specified similar word pairs.

Finally, sentences can only be similar if they have the same number of words. So a sentence likeĀ words1 = ["great"]Ā can never be similar toĀ words2 = ["doubleplus","good"].

Note:

  • The length ofĀ words1Ā andĀ words2Ā will not exceedĀ 1000.
  • The length ofĀ pairsĀ will not exceedĀ 2000.
  • The length of eachĀ pairs[i]Ā will beĀ 2.
  • The length of eachĀ words[i]Ā andĀ pairs[i][j]Ā will be in the rangeĀ [1, 20].


题ē›®å¤§ę„ļ¼š

ē»™ä½ äø¤äøŖ叄子ļ¼ˆē”±å•čÆę•°ē»„č”Øē¤ŗļ¼‰å’Œäø€äŗ›čæ‘义čƍåƹļ¼Œé—®ä½ čæ™äø¤äøŖ叄子ę˜Æ否ē›øä¼¼ļ¼Œå³ęƏē»„ē›øåƹåŗ”ēš„单čÆéƒ½č¦ē›øä¼¼ć€‚

ę³Øꄏē›øä¼¼ę€§åÆ仄传递ļ¼ŒęƔ如åŖē»™ä½ ”great”和”fine”ē›øä¼¼ć€”fine”和”good”ē›øä¼¼ļ¼Œčƒ½ęŽØꖭ”great”和”good”也ē›øä¼¼ć€‚

Idea:

DFS / Union Find

Solution1:

Time complexity: O(|Pairs| * |words1|)

Space complexity: O(|Pairs|)

C++ / DFS

Time complexity: O(|Pairs| + |words1|)

Space complexity: O(|Pairs|)

C++ / DFS Optimized

 

Solution2:

Time complexity: O(|Pairs| + |words1|)

Space complexity: O(|Pairs|)

C++ / Union Find

 

C++ / Union Find, Optimized

Related Problems:

花花酱 LeetCode 734. Sentence Similarity

Problem:

Given two sentencesĀ words1, words2Ā (each represented as an array of strings), and a list of similar word pairsĀ pairs, determine if two sentences are similar.

For example, “great acting skills” and “fine drama talent” are similar, if the similar word pairs areĀ pairs = [["great", "fine"], ["acting","drama"], ["skills","talent"]].

Note that the similarity relation is not transitive. For example, if “great” and “fine” are similar, and “fine” and “good” are similar, “great” and “good” areĀ notĀ necessarily similar.

However, similarity is symmetric. For example, “great” and “fine” being similar is the same as “fine” and “great” being similar.

Also, a word is always similar with itself. For example, the sentencesĀ words1 = ["great"], words2 = ["great"], pairs = []Ā are similar, even though there are no specified similar word pairs.

Finally, sentences can only be similar if they have the same number of words. So a sentence likeĀ words1 = ["great"]Ā can never be similar toĀ words2 = ["doubleplus","good"].

Note:

  • The length ofĀ words1Ā andĀ words2Ā will not exceedĀ 1000.
  • The length ofĀ pairsĀ will not exceedĀ 2000.
  • The length of eachĀ pairs[i]Ā will beĀ 2.
  • The length of eachĀ words[i]Ā andĀ pairs[i][j]Ā will be in the rangeĀ [1, 20].


题ē›®å¤§ę„ļ¼š

ē»™ä½ äø¤äøŖ叄子ļ¼ˆē”±å•čÆę•°ē»„č”Øē¤ŗļ¼‰å’Œäø€äŗ›čæ‘义čƍåƹļ¼Œé—®ä½ čæ™äø¤äøŖ叄子ę˜Æ否ē›øä¼¼ļ¼Œå³ęƏē»„ē›øåƹåŗ”ēš„单čÆéƒ½č¦ē›øä¼¼ć€‚

ę³Øꄏē›øä¼¼ę€§äøčƒ½ä¼ é€’ļ¼ŒęƔ如ē»™åŖä½ ”great”和”fine”ē›øä¼¼ć€”fine”和”good”ē›øä¼¼ļ¼Œčæ™ē§ęƒ…况äø‹”great”和”good”äøē›øä¼¼ć€‚

Idea:

Use hashtable to store mapping from word to its similar words.

Solution: HashTable

Time complexity: O(|pairs| + |words1|)

Space complexity:Ā O(|pairs|)

C++

Java

Python

Related Problems: