1117. Building H2O LeetCode Solution
In this guide, you will get 1117. Building H2O LeetCode Solution with the best time and space complexity. The solution to Building HO problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Building HO solution in C++
- Building HO solution in Java
- Building HO solution in Python
- Additional Resources

Problem Statement of Building HO
There are two kinds of threads: oxygen and hydrogen. Your goal is to group these threads to form water molecules.
There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given releaseHydrogen and releaseOxygen methods respectively, which will allow them to pass the barrier. These threads should pass the barrier in groups of three, and they must immediately bond with each other to form a water molecule. You must guarantee that all the threads from one molecule bond before any other threads from the next molecule do.
In other words:
If an oxygen thread arrives at the barrier when no hydrogen threads are present, it must wait for two hydrogen threads.
If a hydrogen thread arrives at the barrier when no other threads are present, it must wait for an oxygen thread and another hydrogen thread.
We do not have to worry about matching the threads up explicitly; the threads do not necessarily know which other threads they are paired up with. The key is that threads pass the barriers in complete sets; thus, if we examine the sequence of threads that bind and divide them into groups of three, each group should contain one oxygen and two hydrogen threads.
Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.
Example 1:
Input: water = “HOH”
Output: “HHO”
Explanation: “HOH” and “OHH” are also valid answers.
Example 2:
Input: water = “OOHHHH”
Output: “HHOHHO”
Explanation: “HOHHHO”, “OHHHHO”, “HHOHOH”, “HOHHOH”, “OHHHOH”, “HHOOHH”, “HOHOHH” and “OHHOHH” are also valid answers.
Constraints:
3 * n == water.length
1 <= n <= 20
water[i] is either 'H' or 'O'.
There will be exactly 2 * n 'H' in water.
There will be exactly n 'O' in water.
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
1117. Building H2O LeetCode Solution in C++
// LeetCode doesn't support C++20 yet, so we don't have std::counting_semaphore
// or binary_semaphore.
#include <semaphore.h>
class H2O {
public:
H2O() {
sem_init(&hSemaphore, /*pshared=*/0, /*value=*/1);
sem_init(&oSemaphore, /*pshared=*/0, /*value=*/0);
}
~H2O() {
sem_destroy(&hSemaphore);
sem_destroy(&oSemaphore);
}
void hydrogen(function<void()> releaseHydrogen) {
sem_wait(&hSemaphore);
++h;
// releaseHydrogen() outputs "H". Do not change or remove this line.
releaseHydrogen();
if (h % 2 == 0)
sem_post(&oSemaphore);
else
sem_post(&hSemaphore);
}
void oxygen(function<void()> releaseOxygen) {
sem_wait(&oSemaphore);
// releaseOxygen() outputs "O". Do not change or remove this line.
releaseOxygen();
sem_post(&hSemaphore);
}
private:
sem_t hSemaphore;
sem_t oSemaphore;
int h = 0;
};
/* code provided by PROGIEZ */
1117. Building H2O LeetCode Solution in Java
N/A
// code provided by PROGIEZ
1117. Building H2O LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.