Press "Enter" to skip to content

Posts published in “String”

花花酱 LeetCode 539. Minimum Time Difference

Problem

Given a list of 24-hour clock time points in “Hour:Minutes” format, find the minimumĀ minutesĀ difference between any two time points in the list.

Example 1:

Input: ["23:59","00:00"]
Output: 1

Note:

  1. The number of time points in the given list is at least 2 and won’t exceed 20000.
  2. The input time is legal and ranges from 00:00 to 23:59.

Solution

Time complexity: O(nlog1440)

Space complexity: O(n)

C++

 

花花酱 LeetCode 609. Find Duplicate File in System

Problem

https://leetcode.com/problems/find-duplicate-file-in-system/description/

题ē›®å¤§ę„ļ¼šč¾“å‡ŗē³»ē»Ÿäø­ę–‡ä»¶å†…容ē›ø同ēš„ę–‡ä»¶åć€‚

Given a list of directory info including directory path, and all the files with contents in this directory, you need to find out all the groups of duplicate files in the file system in terms of their paths.

A group of duplicate files consists of at leastĀ twoĀ files that have exactly the same content.

A single directory info string in theĀ inputĀ list has the following format:

"root/d1/d2/.../dm f1.txt(f1_content) f2.txt(f2_content) ... fn.txt(fn_content)"

It means there areĀ nĀ files (f1.txt,Ā f2.txtĀ …Ā fn.txtĀ with contentĀ f1_content,Ā f2_contentĀ …Ā fn_content, respectively) in directoryĀ root/d1/d2/.../dm. Note that n >= 1 and m >= 0. If m = 0, it means the directory is just the root directory.

TheĀ outputĀ is a list of group of duplicate file paths. For each group, it contains all the file paths of the files that have the same content. A file path is a string that has the following format:

"directory_path/file_name.txt"

Example 1:

Input:
["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
Output:  
[["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt","root/c/3.txt"]]

Note:

  1. No order is required for the final output.
  2. You may assume the directory name, file name and file content only has letters and digits, and the length of file content is in the range of [1,50].
  3. The number of files given is in the range of [1,20000].
  4. You may assume no files or directories share the same name in the same directory.
  5. You may assume each given directory info represents a unique directory. Directory path and file info are separated by a single blank space.

Follow-up beyond contest:

  1. Imagine you are given a real file system, how will you search files? DFS or BFS?
  2. If the file content is very large (GB level), how will you modify your solution?
  3. If you can only read the file by 1kb each time, how will you modify your solution?
  4. What is the time complexity of your modified solution? What is the most time-consuming part and memory consuming part of it? How to optimize?
  5. How to make sure the duplicated files you find are not false positive?

Solution: HashTable

Key: content, Value: Array of filenames

Time complexity: O(n)

Space complexity: O(n)

C++

 

花花酱 LeetCode 800. Simple RGB Color

Problem

In the following, every capital letter represents some hexadecimal digit fromĀ 0Ā toĀ f.

The red-green-blue colorĀ "#AABBCC"Ā can be writtenĀ asĀ "#ABC"Ā inĀ shorthand.Ā  For example,Ā "#15c"Ā is shorthand for the colorĀ "#1155cc".

Now, say the similarity between two colorsĀ "#ABCDEF"Ā andĀ "#UVWXYZ"Ā isĀ -(AB - UV)^2 -Ā (CD - WX)^2 -Ā (EF - YZ)^2.

Given the colorĀ "#ABCDEF", return a 7 character colorĀ that is most similar toĀ #ABCDEF, and has a shorthand (that is, it can be represented as someĀ "#XYZ"

Example 1:
Input: color = "#09f166"
Output: "#11ee66"
Explanation:  
The similarity is -(0x09 - 0x11)^2 -(0xf1 - 0xee)^2 - (0x66 - 0x66)^2 = -64 -9 -0 = -73.
This is the highest among any shorthand color.

Note:

  • colorĀ is a string of lengthĀ 7.
  • colorĀ is a valid RGB color: forĀ i > 0,Ā color[i]Ā is a hexadecimal digit fromĀ 0Ā toĀ f
  • Any answer which has the same (highest)Ā similarity as the best answer will be accepted.
  • All inputs and outputs should use lowercase letters, and the output is 7 characters.

Solution: Brute Force

R, G, B are independent, find the closest color for each channel separately.

Time complexity: O(3 * 16)

Space complexity: O(1)

 

花花酱 LeetCode 415. Add Strings

Problem

Given two non-negative integersĀ num1Ā andĀ num2Ā represented as string, return the sum ofĀ num1Ā andĀ num2.

Note:

  1. The length of bothĀ num1Ā andĀ num2Ā is < 5100.
  2. BothĀ num1Ā andĀ num2Ā contains only digitsĀ 0-9.
  3. BothĀ num1Ā andĀ num2Ā does not contain any leading zero.
  4. YouĀ must not use any built-in BigInteger libraryĀ orĀ convert the inputs to integerĀ directly.

Solution: Brute Force

Time complexity: O(n)

Space complexity: O(n)

C++

 

花花酱 LeetCode 392. Is Subsequence

题ē›®å¤§ę„ļ¼šé—®sę˜Æäøę˜Ætēš„子åŗåˆ—怂

Problem:

https://leetcode.com/problems/is-subsequence/description/

Given a stringĀ sĀ and a stringĀ t, check ifĀ sĀ is subsequence ofĀ t.

You may assume that there is only lower case English letters in bothĀ sĀ andĀ t.Ā tĀ is potentially a very long (length ~= 500,000) string, andĀ sis a short string (<=100).

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,Ā "ace"Ā is a subsequence ofĀ "abcde"Ā whileĀ "aec"Ā is not).

Example 1:
sĀ =Ā "abc",Ā tĀ =Ā "ahbgdc"

ReturnĀ true.

Example 2:
sĀ =Ā "axc",Ā tĀ =Ā "ahbgdc"

ReturnĀ false.

Follow up:
If there are lots of incoming S, say S1, S2, … , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

Solution: Brute force

Time Complexity: O(|s| + |t|)

Space Complexity: O(1)

C++