Press "Enter" to skip to content

Posts tagged as “string”

花花酱 LeetCode 944. Delete Columns to Make Sorted

Problem

We are given an arrayĀ AĀ ofĀ NĀ lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have a stringĀ "abcdef"Ā and deletion indicesĀ {0, 2, 3}, then the final string after deletionĀ isĀ "bef".

Suppose we chose a set of deletion indicesĀ DĀ such that after deletions, each remaining column in A is inĀ non-decreasingĀ sorted order.

Formally, theĀ c-th column isĀ [A[0][c], A[1][c], ..., A[A.length-1][c]]

Return the minimum possible value ofĀ D.length.

Example 1:

Input: ["cba","daf","ghi"]
Output: 1

Example 2:

Input: ["a","b"]
Output: 0

Example 3:

Input: ["zyx","wvu","tsr"]
Output: 3

Note:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 1000

Solution: Simulation

Check descending case column by column.

Time complexity: O(NL)

Space complexity: O(1)

C++

花花酱 LeetCode 937. Reorder Log Files

Problem

https://leetcode.com/problems/reorder-log-files/description/

You have an array ofĀ logs.Ā  Each log is a space delimited string of words.

For each log, the first word in each log is an alphanumericĀ identifier.Ā  Then, either:

  • Each word after the identifier will consist only of lowercase letters, or;
  • Each word after the identifier will consist only of digits.

We will call these two varieties of logsĀ letter-logsĀ andĀ digit-logs.Ā  It is guaranteed that each log has at least one word after its identifier.

Reorder the logs so that all of the letter-logs come before any digit-log.Ā  The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties.Ā  The digit-logs should be put in their original order.

Return the final order of the logs.

Example 1:

Input: ["a1 9 2 3 1","g1 act car","zo4 4 7","ab1 off key dog","a8 act zoo"]
Output: ["g1 act car","a8 act zoo","ab1 off key dog","a1 9 2 3 1","zo4 4 7"]

Note:

  1. 0 <= logs.length <= 100
  2. 3 <= logs[i].length <= 100
  3. logs[i]Ā is guaranteed to have an identifier, and a word after the identifier.

Solution: Partition + Sort

  1. partition the array such that all digit logs are after all letter logs
  2. sort the letter logs part based on the log content

Time complexity: O(n + aloga)

Space complexity: O(n)

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 925. Long Pressed Name

Problem

Your friend is typing hisĀ nameĀ into a keyboard.Ā  Sometimes, when typing a characterĀ c, the key might getĀ long pressed, and the character will be typed 1 or more times.

You examine theĀ typedĀ characters of the keyboard.Ā  ReturnĀ TrueĀ if it is possible that it was your friends name, with some characters (possibly none) being long pressed.

Example 1:

Input: name = "alex", typed = "aaleex"
Output: true
Explanation: 'a' and 'e' in 'alex' were long pressed.

Example 2:

Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.

Example 3:

Input: name = "leelee", typed = "lleeelee" 
Output: true

Example 4:

Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It's not necessary to long press any character.

Note:

  1. name.length <= 1000
  2. typed.length <= 1000
  3. The characters ofĀ nameĀ andĀ typedĀ are lowercase letters.

Solution: Two Pointers

Time complexity: O(n)

Space complexity: O(1)

C++

C++ / Iterator