Press "Enter" to skip to content

Posts published in “Hashtable”

花花酱 LeetCode 953. Verifying an Alien Dictionary

Problem

In an alien language, surprisingly they also use english lowercase letters, but possiblyĀ in a differentĀ order. TheĀ orderĀ of the alphabetĀ is some permutationĀ of lowercase letters.

Given a sequence ofĀ wordsĀ written in the alien language,Ā and theĀ orderĀ of the alphabet,Ā returnĀ trueĀ if and only if the givenĀ wordsĀ are sorted lexicographicaly in this alien language.

 

Example 1:

Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.

Example 2:

Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.

Example 3:

Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to lexicographical rules "apple" > "app", because 'l' > 'āˆ…', where 'āˆ…' is defined as the blank character which is less than any other character (More info).

Note:

  1. 1 <= words.length <= 100
  2. 1 <= words[i].length <= 20
  3. order.length == 26
  4. All characters inĀ words[i]Ā andĀ orderĀ are english lowercase letters.

Solution: Hashtable

Time complexity: O(sum(len(words[i])))

Space complexity: O(26)

C++

花花酱 LeetCode 648. Replace Words

Problem

https://leetcode.com/problems/replace-words/description/

In English, we have a concept calledĀ root, which can be followed by some other words to form another longer word – let’s call this wordĀ successor. For example, the rootĀ an, followed byĀ other, which can form another wordĀ another.

Now, given a dictionary consisting of many roots and a sentence. You need to replace all theĀ successorĀ in the sentence with theĀ rootĀ forming it. If aĀ successorĀ has manyĀ rootsĀ can form it, replace it with the root with the shortest length.

You need to output the sentence after the replacement.

Example 1:

Input: dict = ["cat", "bat", "rat"]
sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"

Note:

  1. The input will only have lower-case letters.
  2. 1 <= dict words number <= 1000
  3. 1 <= sentence words number <= 1000
  4. 1 <= root length <= 100
  5. 1 <= sentence words length <= 1000

Solution 1: HashTable

Time complexity: O(sum(w^2))

Space complexity: O(sum(l))

Solution2: Trie

Time complexity: O(sum(l) + n)

Space complexity: O(sum(l) * 26)

 

 

花花酱 LeetCode 929. Unique Email Addresses

Every email consists of a local name and a domain name, separated by the @ sign.

For example, inĀ alice@leetcode.com,Ā aliceĀ is the local name, andĀ leetcode.comĀ is the domain name.

Besides lowercase letters, these emails may containĀ '.'s orĀ '+'s.

If you add periods ('.') between some characters in theĀ local nameĀ part of an email address, mail sent there will be forwarded to the same address without dots in the local name.Ā  For example,Ā "alice.z@leetcode.com"Ā andĀ "alicez@leetcode.com"Ā forward to the same email address.Ā  (Note that this rule does not apply for domain names.)

If you add a plus ('+') in theĀ local name, everything after the first plus sign will beĀ ignored. This allows certain emails to be filtered, for exampleĀ m.y+name@email.comĀ will be forwarded toĀ my@email.com.Ā  (Again, this rule does not apply for domain names.)

It is possible to use both of these rules at the same time.

Given a list ofĀ emails, we send one email to each address in the list.Ā Ā How many different addresses actually receive mails?

Example 1:

Input: ["test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"]
Output: 2
Explanation:Ā "testemail@leetcode.com" and "testemail@lee.tcode.com" actually receive mails

 

Note:

  • 1 <= emails[i].lengthĀ <= 100
  • 1 <= emails.length <= 100
  • EachĀ emails[i]Ā contains exactly oneĀ '@'Ā character.

 

Solution:Ā 

Time complexity: O(n*l)
Space complexity: O(n*l)

C++

花花酱 LeetCode 923. 3Sum With Multiplicity

Problem

Given an integer arrayĀ A, and an integerĀ target, return the number ofĀ tuplesĀ i, j, kĀ  such thatĀ i < j < kĀ andĀ A[i] + A[j] + A[k] == target.

As the answer can be very large, return it moduloĀ 10^9 + 7.

Example 1:

Input: A = [1,1,2,2,3,3,4,4,5,5], target = 8
Output: 20
Explanation: 
Enumerating by the values (A[i], A[j], A[k]):
(1, 2, 5) occurs 8 times;
(1, 3, 4) occurs 8 times;
(2, 2, 4) occurs 2 times;
(2, 3, 3) occurs 2 times.

Example 2:

Input: A = [1,1,2,2,2,2], target = 5
Output: 12
Explanation: 
A[i] = 1, A[j] = A[k] = 2 occurs 12 times:
We choose one 1 from [1,1] in 2 ways,
and two 2s from [2,2,2,2] in 6 ways.

Note:

  1. 3 <= A.length <= 3000
  2. 0 <= A[i] <= 100
  3. 0 <= target <= 300

Solution: Math / Combination

Time complexity: O(n + |target|^2)

Space complexity: O(|target|)

C++

花花酱 LeetCode 916. Word Subsets

Problem

We are given two arraysĀ AĀ andĀ BĀ of words.Ā  Each word is a string of lowercase letters.

Now, say thatĀ wordĀ bĀ is a subset of wordĀ aĀ if every letter inĀ bĀ occurs inĀ a,Ā including multiplicity.Ā  For example,Ā "wrr"Ā is a subset ofĀ "warrior", but is not a subset ofĀ "world".

Now say a wordĀ aĀ fromĀ AĀ isĀ universalĀ if for everyĀ bĀ inĀ B,Ā bĀ is a subset ofĀ a.

Return a list of all universal words inĀ A.Ā  You can return the words in any order.

Example 1:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
Output: ["facebook","google","leetcode"]

Example 2:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
Output: ["apple","google","leetcode"]

Example 3:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
Output: ["facebook","google"]

Example 4:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
Output: ["google","leetcode"]

Example 5:

Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["ec","oc","ceo"]
Output: ["facebook","leetcode"]

Note:

  1. 1 <= A.length, B.length <= 10000
  2. 1 <= A[i].length, B[i].lengthĀ <= 10
  3. A[i]Ā andĀ B[i]Ā consist only of lowercase letters.
  4. All words inĀ A[i]Ā are unique: there isn’tĀ i != jĀ withĀ A[i] == A[j].

Solution: Hashtable

Find the max requirement for each letter from B.

Time complexity: O(|A|+|B|)

Space complexity: O(26)

C++