Press "Enter" to skip to content

Huahua's Tech Road

花花酱 LeetCode 872. Leaf-Similar Trees

Problem

Consider all the leaves of a binary tree.Ā  FromĀ left to right order, the values of thoseĀ leaves form aĀ leaf value sequence.

For example, in the given tree above, the leaf value sequence isĀ (6, 7, 4, 9, 8).

Two binary trees are consideredĀ leaf-similarĀ if their leaf value sequence is the same.

ReturnĀ trueĀ if and only if the two given trees with head nodesĀ root1Ā andĀ root2Ā are leaf-similar.

Note:

  • Both of the given trees will have betweenĀ 1Ā andĀ 100Ā nodes.

Solution: Recursion

Time complexity: O(n1 + n2)

Space complexity: O(n1 + n2)

Python

 

花花酱 LeetCode 712. Minimum ASCII Delete Sum for Two Strings

Problem

Given two stringsĀ s1, s2, find the lowest ASCII sum of deleted characters to make two strings equal.

Example 1:

Input: s1 = "sea", s2 = "eat"
Output: 231
Explanation: Deleting "s" from "sea" adds the ASCII value of "s" (115) to the sum.
Deleting "t" from "eat" adds 116 to the sum.
At the end, both strings are equal, and 115 + 116 = 231 is the minimum sum possible to achieve this.

Example 2:

Input: s1 = "delete", s2 = "leet"
Output: 403
Explanation: Deleting "dee" from "delete" to turn the string into "let",
adds 100[d]+101[e]+101[e] to the sum.  Deleting "e" from "leet" adds 101[e] to the sum.
At the end, both strings are equal to "let", and the answer is 100+101+101+101 = 403.
If instead we turned both strings into "lee" or "eet", we would get answers of 433 or 417, which are higher.

Note:

  • 0 < s1.length, s2.length <= 1000.
  • All elements of each string will have an ASCII value inĀ [97, 122].

Solution: DP

Time complexity: O(l1 * l2)

Space complexity:Ā O(l1 * l2)

C++

Solution2: Recursion + Memorization

Time complexity: O(l1 * l2)

Space complexity: O(l1 * l2)

C++

Related Problems

 

花花酱 LeetCode 115. Distinct Subsequences

Problem

Given a stringĀ SĀ and a stringĀ T, count the number of distinct subsequences ofĀ SĀ which equalsĀ T.

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:

Input: S = "rabbbit", T = "rabbit"
Output:Ā 3
Explanation:  As shown below, there are 3 ways you can generate "rabbit" from S. (The caret symbol ^ means the chosen letters) 
rabbbit
^^^^ ^^
rabbbit
^^ ^^^^
rabbbit
^^^ ^^^

Example 2:

Input: S = "babgbag", T = "bag"
Output:Ā 5
Explanation:  As shown below, there are 5 ways you can generate "bag" from S. (The caret symbol ^ means the chosen letters)
babgbag
^^ ^
babgbag
^^ ^
babgbag
^ ^^
babgbag
^ ^^
babgbag
^^^

Solution: DP

Time complexity: O(|s| * |t|)

Space complexity: O(|s| * |t|)

C++

Related Problems:

花花酱 LeetCode 836. Rectangle Overlap

Problem

A rectangle isĀ represented as aĀ listĀ [x1, y1, x2, y2], whereĀ (x1, y1)Ā are the coordinates of its bottom-left corner, andĀ (x2,Ā y2)Ā are the coordinates of its top-right corner.

Two rectangles overlap if the area of their intersection is positive.Ā  To be clear, two rectangles that only touch at the corner or edges do not overlap.

Given two (axis-aligned) rectangles, return whetherĀ they overlap.

Example 1:

Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true

Example 2:

Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false

Notes:

  1. Both rectanglesĀ rec1Ā andĀ rec2Ā are lists of 4 integers.
  2. All coordinates in rectangles will be betweenĀ -10^9Ā andĀ 10^9.

Solution: Geometry

Time complexity: O(1)

Space complexity: O(1)

 

花花酱 LeetCode 345. Reverse Vowels of a String

Problem

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = “hello”, return “holle”.

Example 2:
Given s = “leetcode”, return “leotcede”.

Note:
The vowels does not include the letter “y”.

Solution