Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode 811. Subdomain Visit Count


A website domain like “” consists of various subdomains. At the top level, we have “com”, at the next level, we have “”, and at the lowest level, “”. When we visit a domain like “”, we will also visit the parent domains “” and “com” implicitly.

Now, call a “count-paired domain” to be a count (representing the number of visits this domain received), followed by a space, followed by the address. An example of a count-paired domain might be “9001”.

We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains, (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.

Example 1:
["9001", "9001", "9001 com"]
We only have one website domain: "". As discussed above, the subdomain "" and "com" will also be visited. So they will all be visited 9001 times.

Example 2:
["900", "50", "1", "5"]
["901","50","900","5","5 org","1","951 com"]
We will visit "" 900 times, "" 50 times, "" once and "" 5 times. For the subdomains, we will visit "" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.


  • The length of cpdomains will not exceed 100.
  • The length of each domain name will not exceed 100.
  • Each address will have either 1 or 2 “.” characters.
  • The input count in any count-paired domain will not exceed 10000.

Solution: HashTable

Time complexity: O(n)

Space complexity: O(n)



花花酱 LeetCode 593. Valid Square



Given the coordinates of four points in 2D space, return whether the four points could construct a square.

The coordinate (x,y) of a point is represented by an integer array with two integers.


Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True


  1. All the input integers are in the range [-10000, 10000].
  2. A valid square has four equal sides with positive length and four equal angles (90-degree angles).
  3. Input points have no order.


Time complexity: O(1)

Space complexity: O(1)



花花酱 LeetCode 192. Word Frequency

Write a bash script to calculate the frequency of each word in a text file words.txt.

For simplicity sake, you may assume:

  • words.txt contains only lowercase characters and space ' ' characters.
  • Each word must consist of lowercase characters only.
  • Words are separated by one or more whitespace characters.

For example, assume that words.txt has the following content:

the day is sunny the the
the sunny is is

Your script should output the following, sorted by descending frequency:

the 4
is 3
sunny 2
day 1

Don’t worry about handling ties, it is guaranteed that each word’s frequency count is unique.



花花酱 LeetCode 806. Number of Lines To Write String



We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 units, it is written on the next line. We are given an array widths, an array where widths[0] is the width of ‘a’, widths[1] is the width of ‘b’, …, and widths[25] is the width of ‘z’.

Now answer two questions: how many lines have at least one character from S, and what is the width used by the last such line? Return your answer as an integer list of length 2.

Example :
widths = [10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "abcdefghijklmnopqrstuvwxyz"
Output: [3, 60]
All letters have the same length of 10. To write all 26 letters,
we need two full lines and one line with 60 units.
Example :
widths = [4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10]
S = "bbbcccdddaaa"
Output: [2, 4]
All letters except 'a' have the same length of 10, and 
"bbbcccdddaa" will cover 9 * 10 + 2 * 4 = 98 units.
For the last 'a', it is written on the second line because
there is only 2 units left in the first line.
So the answer is 2 lines, plus 4 units in the second line.


  • The length of S will be in the range [1, 1000].
  • S will only contain lowercase letters.
  • widths is an array of length 26.
  • widths[i] will be in the range of [2, 10].

Solution: Simulation

Time Complexity: O(n)

Space Complexity: O(1)