title: How to get all the possible winning combination of 17 tile mahjong hand
body: <p>I'm trying to create a logic to analyze a winning hand in Mahjong and list out all the possible combination of a winning hand.</p>
  <p>Let me start with some background of the game:</p>
  <ul>
  <li><p>In MahJong there are 4 type of tiles - Dots, Bamboos, Characters and Honors Tiles</p>
  </li>
  <li><p>(D)ots, (B)amboos and (C)haracters come in numbers of 1-9</p>
  </li>
  <li><p>Honers consist of 7 unique tiles (namely North, East, South, West, Green, Red and White)</p>
  </li>
  <li><p>Pong is getting three of a kind</p>
  </li>
  <li><p>Chow is consecutive three tiles of the same suit, i.e. 2 Dot, 3 Dot, 4 Dot
  (skipping kong here to make this less complicated)</p>
  </li>
  <li><p>To get a winning hand, you will need five combinations of Pong or Chow plus a extra pair (skipping the special winning hand here to make this less complicated)</p>
  </li>
  </ul>
  <p>here is an example of a winning hand</p>
  <p>2D(ot) 3D 4D ; 5D 5D 5D ; Red Red Red; 7D 8D 9D; 1C(haracter) 2C 3C ; White White</p>
  <hr />
  <p>While above is obvious and have only one unique solution, sometimes there will be cased where multiple combination is possible, just like below hand</p>
  <p>1D 1D 1D 2D 2D 2D 3D 3D 3D 4D 4D 4D 5D 5D 5D 6D 6D</p>
  <p>this can be interpreted easily as 5 Pong + a pair of 6D winning hand</p>
  <p>111; 222; 333; 444; 555; 66</p>
  <p>but it is also possible to be a winning hand of</p>
  <p>123; 123; 123; 444; 555; 66</p>
  <p>111; 222; 33; 345; 456; 456</p>
  <hr />
  <p>Now onto my goal -</p>
  <p>since different winning hand will score different points, I'm trying to build a logic to test the 17 tile hand and compare all the possible winning combination to get the highest score hand. and this is where I am stuck in, my current logic is:</p>
  <pre><code>1. for each tile in hand, search for each other tile and see if can form combination
  2. remove the matched 3 tile from the array and add them to the winning hand array
  3. repeat step (1) for the rest of the tile
  4. only the pair should be left - add this to winning hand array and get winning hand[1]
  
  </code></pre>
  <p>after this I'm stuck - what can I do to avoid my loop to get the same ouput? i can't just ask the code to skip the 1D 1D 1D pong because as illustrate above it's also possible for other winning hand to have that pattern. and it'll be also super slow if I brute force all the 17! possible combination and filter out the non winning hand.</p>
  <p>Would be grateful if anyone can help me with a logic flow! Thanks</p>
  <p>P.S. and if it matters I'm doing this in js</p>