Press "Enter" to skip to content

Posts tagged as “easy”

花花酱 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 942. DI String Match

Problem

Given a stringĀ SĀ thatĀ onlyĀ contains “I” (increase) or “D” (decrease), letĀ N = S.length.

ReturnĀ anyĀ permutationĀ AĀ ofĀ [0, 1, ..., N]Ā such that for allĀ i = 0,Ā ..., N-1:

  • IfĀ S[i] == "I", thenĀ A[i] < A[i+1]
  • IfĀ S[i] == "D", thenĀ A[i] > A[i+1]

Example 1:

Input: "IDID"
Output: [0,4,1,3,2]

Example 2:

Input: "III"
Output: [0,1,2,3]

Example 3:

Input: "DDI"
Output: [3,2,0,1]

Note:

  1. 1 <= S.length <= 10000
  2. SĀ only contains charactersĀ "I"Ā orĀ "D".

Solution: Greedy

“I” -> use the smallest possible number

“D” -> use the largest possible number

Time complexity: O(n)

Space complexity: O(n)

C++

花花酱 LeetCode 941. Valid Mountain Array

Problem

Given an arrayĀ AĀ of integers, returnĀ trueĀ if and only if it is aĀ valid mountain array.

Recall that A is a mountain array if and only if:

  • A.length >= 3
  • There exists someĀ iĀ withĀ 0 < iĀ < A.length - 1Ā such that:
    • A[0] < A[1] < ... A[i-1] < A[i]
    • A[i] > A[i+1] > ... > A[B.length - 1]

Example 1:

Input: [2,1]
Output: false

Example 2:

Input: [3,5,5]
Output: false

Example 3:

Input: [0,3,2,1]
Output: true

Note:

  1. 0 <= A.length <= 10000
  2. 0 <= A[i] <= 10000Ā 

Solution

Use has_up and has_down to track whether we have A[i] > A[i – 1] and A[i] < A[i – 1] receptively.

return false if any of the following happened:

  1. size(A) < 3
  2. has_down happened before has_up
  3. not has_down or not has_up
  4. A[i – 1] < A[i] after has_down
  5. A[i – 1] > A[i] before has_up

Time complexity: O(n)

Space complexity: O(n)

C++

花花酱 LeetCode 933. Number of Recent Calls

Problem

Write a classĀ RecentCounterĀ to count recent requests.

It has only one method:Ā ping(int t), where t represents some time in milliseconds.

Return the number ofĀ pings that have been made from 3000 milliseconds ago until now.

Any ping with time inĀ [t - 3000, t]Ā will count, including the current ping.

It is guaranteed that every call toĀ pingĀ uses a strictly larger value ofĀ tĀ than before.

Example 1:

Input: inputs = ["RecentCounter","ping","ping","ping","ping"], inputs = [[],[1],[100],[3001],[3002]]
Output: [null,1,2,3,3]

Note:

  1. Each test case will have at mostĀ 10000Ā calls toĀ ping.
  2. Each test case will callĀ pingĀ with strictly increasing values ofĀ t.
  3. Each call to ping will haveĀ 1 <= t <= 10^9.

Solution: Queue

Use a FIFO queue to track all the previous pings that are within 3000 ms to current.

Time complexity: Avg O(1), Total O(n)

Space complexity: O(n)

C++

花花酱 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++