LeetCode 760. Find Anagram Mappings in Java

In this post we will solve LeetCode 760. Find Anagram Mappings problem using the Java programming language.

You are given two integer arrays nums1 and nums2 where nums2 is an anagram of nums1. 
Both arrays may contain duplicates.

Return an index mapping array mapping from nums1 to nums2 where 
mapping[i] = j means the ith element in nums1 appears in nums2 at index j. 
If there are multiple answers, return any of them.

An array a is an anagram of an array b means b is made by randomizing the order of the elements in a.

Constraints:

o 1 <= nums1.length <= 100
o nums2.length == nums1.length
o 0 <= nums1[i], nums2[i] <= 10^5
o nums2 is an anagram of nums1.

Related Topics:

o Array
o Hash Table

We are provided with two int[] holding two anagrams. We need to write a function that returns an index mapping array mapping from nums1 to nums2 where mapping[i] = j means the ith element in nums1 appears in nums2 at index j. Continue reading “LeetCode 760. Find Anagram Mappings in Java”

LeetCode 235. Lowest Common Ancestor of a Binary Tree in Java

In this post we will solve LeetCode 235. Lowest Common Ancestor of a Binary Tree problem using Java.

Given a binary search tree (BST), 
find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: 
“The lowest common ancestor is defined between two nodes p and q 
as the lowest node in T that has both p and q as descendants 
(where we allow a node to be a descendant of itself).”

Constraints:

o The number of nodes in the tree is in the range [2, 10^5].
o -10^9 <= Node.val <= 10^9
o All Node.val are unique.
o p != q
o p and q will exist in the BST.

Realated Topics:

o Tree
o Depth-First Search
o Binary Search Tree
o Binary Trea

We are given a Binary Search Tree (BST) and the pointers / references to two nodes in the tree. We need to return the Lowest Common Ancestor (LCA) node. Continue reading “LeetCode 235. Lowest Common Ancestor of a Binary Tree in Java”

LeetCode 1156. Swap For Longest Repeated Character Substring in Java

In this post we will solve the LeetCode 1156. Swap For Longest Repeated Character Substring problem using the Java programming language.

You are given a string text. You can swap two of the characters in the text.

Return the length of the longest substring with repeated characters.

Constraints:

o 1 <= text.length <= 2 * 10^4
o text consist of lowercase English characters only.

Related Topics:

o String
o Sliding Window

We are given a string of English lowercase characters and we can swap two characters once in order to return the longest count of consecutive characters. Continue reading “LeetCode 1156. Swap For Longest Repeated Character Substring in Java”

LeetCode 1347 Minimum Number of Steps to Make Two Strings Anagram in Java

In this post we will solve the LeetCode 1347 Minimum Number of Steps to Make Two Strings Anagram problem using Java.

You are given two strings of the same length s and t. 
In one step you can choose any character of t and replace it with another character.

Return the minimum number of steps to make t an anagram of s.

An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.

Constraints:

o 1 <= s.length <= 5 * 10^4
o s.length == t.length
o s and t consist of lowercase English letters only.

Related Topics:

o Hash Table
o String

It seems that in this problem it is needed to replace the minimum number of characters in string `t` with lowercase characters from the English alphabet in order to make `t` an anagram of string `s`. Continue reading “LeetCode 1347 Minimum Number of Steps to Make Two Strings Anagram in Java”

LeetCode 1518. Water Bottles in Java

In this post we will solve LeetCode 1518. Water Bottles problem using the Java programming language.

There are numBottles water bottles that are initially full of water. 
You can exchange numExchange empty water bottles from the market with one full water bottle.

The operation of drinking a full water bottle turns it into an empty bottle.

Given the two integers numBottles and numExchange, 
return the maximum number of water bottles you can drink.

Constraints:

o 1 <= numBottles <= 100
o 2 <= numExchange <= 100

Related Topics:

o Math
o Simulation

Before you start solving a problem associated with a website make sure you read the current description directly from the site in order to get the current description of the problem. On occasions descriptions may be updated providing additional information that may help you come up with a better solution faster. Continue reading “LeetCode 1518. Water Bottles in Java”

LeetCode 160 Intersection of Two Linked Lists in Java

In this post I will be solving LeetCode 160 Intersection of Two Linked Lists using the Java programming language.

Given the heads of two singly linked-lists headA and headB, 
return the node at which the two lists intersect. 
If the two linked lists have no intersection at all, return null.

Custom Judge:

The inputs to the judge are given as follows (your program is not given these inputs):

o intersectVal - The value of the node where the intersection occurs.
  This is 0 if there is no intersected node.
o listA - The first linked list.
o listB - The second linked list.
o skipA - The number of nodes to skip ahead in listA (starting from the head) 
  to get to the intersected node.
o skipB - The number of nodes to skip ahead in listB (starting from the head) 
  to get to the intersected node.

The judge will then create the linked structure based on these inputs and pass the two heads, 
headA and headB to your program.
If you correctly return the intersected node,
then your solution will be accepted.

Constraints:

o The number of nodes of listA is in the m.
o The number of nodes of listB is in the n.
o 1 <= m, n <= 3 * 104
o 1 <= Node.val <= 105
o 0 <= skipA < m
o 0 <= skipB < n
o intersectVal is 0 if listA and listB do not intersect.
o intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.

Related Topics:

* Hash Table
o Linked List
* Two Pointers

Follow up:

* Could you write a solution that runs in O(m + n) time and use only O(1) memory?

If interested I suggest you get the current description from LeetCode before you start coding. Continue reading “LeetCode 160 Intersection of Two Linked Lists in Java”

LeetCode 223 Rectangle Area in Java

In this post we will solve LeetCode 223 Rectangle Area in Java.

Given the coordinates of two rectilinear rectangles in a 2D plane, 
return the total area covered by the two rectangles.

The first rectangle is defined by its bottom-left corner (ax1, ay1) and its top-right corner (ax2, ay2).

The second rectangle is defined by its bottom-left corner (bx1, by1) and its top-right corner (bx2, by2).

Constraints:

o -10^4 <= ax1, ay1, ax2, ay2, bx1, by1, bx2, by2 <= 10^4

Related Topics:

o Math
o Geometry

We are given the coordinates of two rectilinear rectangles in a 2D plane and are asked to return the total area covered by the two rectangles. Continue reading “LeetCode 223 Rectangle Area in Java”

LeetCode 142 Linked List Cycle II in Java

In this post I will be solving LeetCode 142 Linked List Cycle II using the Java programming language.

This problem seems to be the continuation of a problem we solved earlier LeetCode 141 Linked List Cycle in Java in this blog. Perhaps you would like to read it before attempting to solve the problem in this post.

Given the head of a linked list, 
return the node where the cycle begins.
If there is no cycle, return null.

There is a cycle in a linked list if there is some node in the list that 
can be reached again by continuously following the next pointer. 

Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed).
It is -1 if there is no cycle.
Note that pos is not passed as a parameter.

Do not modify the linked list.

Constraints:

o The number of the nodes in the list is in the range [0, 104].
o -10^5 <= Node.val <= 10^5
o pos is -1 or a valid index in the linked-list.

Related Topics:

o Hash Table
o Linked List
o Two Pointers

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

We are given a singly linked list which may or may not have a cycle. We need to return `null` if it does not contain a cycle, or the node where the cycle begins. The LeetCode web site contains some diagrams to better illustrate the linked lists with a cycle. Continue reading “LeetCode 142 Linked List Cycle II in Java”

LeetCode 141 Linked List Cycle in Java

In this post we will solve the LeetCode 141 Linked List Cycle problem.

Given head, the head of a linked list, 
determine if the linked list has a cycle in it.

There is a cycle in a linked list 
if there is some node in the list that can be reached again by continuously following the next pointer. 

Internally, pos is used to denote the index of the node that tail's next pointer is connected to. 
Note that pos is not passed as a parameter.

Return true if there is a cycle in the linked list. 
Otherwise, return false.

Constraints:

o The number of the nodes in the list is in the range [0, 10^4].
o -10^5 <= Node.val <= 10^5
o pos is -1 or a valid index in the linked-list.

Related Topics:

o Hash Table
o Linked List
o Two Pointers

We are given a singly linked list and are asked to determine if there is a cycle in it. If interested in the problem please take a look at the current description of it in the LeetCode website. Continue reading “LeetCode 141 Linked List Cycle in Java”

LeetCode 328 Odd Even Linked List in Java

In this post I will try three implementations of the function of interest for LeetCode 328 Odd Even Linked List problem using the Java programming language.

Given a singly linked list, 
group all odd nodes together followed by the even nodes.

Please note here we are talking about the node number and not the value in the nodes!!!

You should try to do it in place. 
The program should run in O(1) space complexity and O(nodes) time complexity.

Constraints:

o The relative order inside both the even and odd groups should remain as it was in the input.
o The first node is considered odd, the second node even and so on ...
o The length of the linked list is between [0, 10^4].

We are given a singly linked list. We need to group all `odd` nodes together followed by the `even` nodes. Please note the definition of what makes a node `odd` and `even`. It is not the value in the node but the position of the node in the linked list. Continue reading “LeetCode 328 Odd Even Linked List in Java”