Class H2O

java.lang.Object
g1101_1200.s1117_building_h2o.H2O

public class H2O extends Object
1117 - Building H2O.<p>Medium</p> <p>There are two kinds of threads: <code>oxygen</code> and <code>hydrogen</code>. Your goal is to group these threads to form water molecules.</p> <p>There is a barrier where each thread has to wait until a complete molecule can be formed. Hydrogen and oxygen threads will be given <code>releaseHydrogen</code> and <code>releaseOxygen</code> 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.</p> <p>In other words:</p> <ul> <li>If an oxygen thread arrives at the barrier when no hydrogen threads are present, it must wait for two hydrogen threads.</li> <li>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.</li> </ul> <p>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.</p> <p>Write synchronization code for oxygen and hydrogen molecules that enforces these constraints.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> water = &ldquo;HOH&rdquo;</p> <p><strong>Output:</strong> &ldquo;HHO&rdquo;</p> <p><strong>Explanation:</strong> &ldquo;HOH&rdquo; and &ldquo;OHH&rdquo; are also valid answers.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> water = &ldquo;OOHHHH&rdquo;</p> <p><strong>Output:</strong> &ldquo;HHOHHO&rdquo;</p> <p><strong>Explanation:</strong> &ldquo;HOHHHO&rdquo;, &ldquo;OHHHHO&rdquo;, &ldquo;HHOHOH&rdquo;, &ldquo;HOHHOH&rdquo;, &ldquo;OHHHOH&rdquo;, &ldquo;HHOOHH&rdquo;, &ldquo;HOHOHH&rdquo; and &ldquo;OHHOHH&rdquo; are also valid answers.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>3 * n == water.length</code></li> <li><code>1 <= n <= 20</code></li> <li><code>water[i]</code> is either <code>'H'</code> or <code>'O'</code>.</li> <li>There will be exactly <code>2 * n</code> <code>'H'</code> in <code>water</code>.</li> <li>There will be exactly <code>n</code> <code>'O'</code> in <code>water</code>.</li> </ul>