LeetCode 171. Excel Sheet Column Number in Java

In this post we will solve LeetCode 171Excel Sheet Column Number problem using the Java programming language.

If interested in this problem, I suggest you go to the LeetCode website and read the description of the problem in case something has changed. If you are still interested please read on.

```Given a string columnTitle that represents the column title as appear in an Excel sheet,
return its corresponding column number.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...

Constraints:

o 1 <= columnTitle.length <= 7
o columnTitle consists only of uppercase English letters.
o columnTitle is in the range ["A", "FXSHRXW"].

Related Topics:

o Math
o String
```

The problem is relatively simple. We are given a string of uppercase characters each of which represents a digit in base 26. We need to compute the integer value associated with the string. Continue reading “LeetCode 171. Excel Sheet Column Number in Java”

LeetCode 872. Leaf-Similar Trees in Java

In this post we will be solving LeetCode 872. Leaf-Similar Trees. As usual, if interested, please visit the LeetCode site and read the current version of the problem to make sure all is well before you start developing code.

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

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.

Constraints:

o The number of nodes in each tree will be in the range [1, 200].
o Both of the given trees will have values in the range [0, 200].

Related Topics:

o Tree
o Depth-First Search
o Binary Tree
```

We are given two binary trees. We need to determine if the sequence of leaf nodes match or not. Seems like an in-order tree traversal problem. We could populate a list with the leaf nodes of the first tree and repeat with the nodes of the second tree. Our answer will be based on the contents of both lists. Continue reading “LeetCode 872. Leaf-Similar Trees in Java”

LeetCode 669. Trim a Binary Search Tree in Java

In this post we will solve the LeetCode 669 Trim a Binary Search Tree problem. This problem is ranked a Medium by LeetCode. It took me some extra time to figure out what I had to do.

```Given the root of a binary search tree and the lowest and highest boundaries as low and high,
trim the tree so that all its elements lies in [low, high].
Trimming the tree should not change the relative structure of the elements that will remain in the tree
(i.e., any node's descendant should remain a descendant).
It can be proven that there is a unique answer.

Return the root of the trimmed binary search tree.
Note that the root may change depending on the given bounds.

Constraints:

o The number of nodes in the tree in the range [1, 10^4].
o 0 <= Node.val <= 10^4
o The value of each node in the tree is unique.
o root is guaranteed to be a valid binary search tree.
o 0 <= low <= high <= 10^4

Related Topics:

o Tree
o Depth-First Search
o Binary Search Tree
o Binary Tree
```

The problem clearly states that we are presented with a binary search tree which specifies the relationship between node values. Continue reading “LeetCode 669. Trim a Binary Search Tree in Java”

LeetCode 402. Remove K Digits in Java

In this post we will solve the LeetCode 402 Remove K Digits problem.

```Given string num representing a non-negative integer num, and an integer k,
return the smallest possible integer after removing k digits from num.

Constraints:

o 1 <= k <= num.length <= 10^5
o num consists of only digits.
o num does not have any leading zeros except for the zero itself.
```

If interested in this problem I suggest you read the current description in the LeetCode website. The requirements may be refined since this post. Continue reading “LeetCode 402. Remove K Digits in Java”

LeetCode 75. Sort Colors in Java

In this post we will solve the LeetCode 75. Sort Colors problem using the Java programming language and the VSCode IDE on a Windows computer.

```Given an array nums with n objects colored red, white, or blue,
sort them in-place so that objects of the same color are adjacent,
with the colors in the order red, white, and blue.

We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.

You must solve this problem without using the library's sort function.

Constraints:

o n == nums.length
o 1 <= n <= 300
o nums[i] is 0, 1, or 2.
```

We are given an integer array with values 0, 1, and 2 which represent colors red, white and blue respectively. We must solve the problem without the use of any library sort function. That makes sense because we would only have to sort the input array and return. Continue reading “LeetCode 75. Sort Colors in Java”

LeetCode 229. Majority Element II in Java

In this post we will solve the LeetCode 229. Majority Element II problem using two approaches.

```Given an integer array of size n, find all elements that appear more than floor(n/3)times.

Constraints:

o 1 <= nums.length <= 5 * 10^4
o -10^9 <= nums[i] <= 10^9
```

We are given an array of integer values. We need to find all elements whose values are larger than floor(n / 3), where `n` is the number of elements in the input array. Continue reading “LeetCode 229. Majority Element II in Java”