Files
mrugesh 91df817cfe fix(guide) add stubs, update spellings and prepare for move (#36531)
* fix(guide) add stubs and correct file path misspellings and pr… (#36528)

* fix: corrected file path to match curriculum

* fix: renamed to newer challenge name

* fix: added solutions to articles from challenge files

* fix: added missing .english to file name

* fix: added missing title to guide article

* fix: correct solution for guide article

* fix: replaced stub with hint

* fix: added space in Hint headers

* fix: added solution to guide article

* fix: added solution to guide article

* test: replaced stub with hint and solution

* fix: add Problem number: to title

* fix: changed generatorexponential to correct name

* fix: renamed knight's tour to knights-tour

* fix: updated guide article
2019-07-30 00:25:58 +05:30

1.1 KiB

title
title
State name puzzle

State name puzzle


Solutions

Solution 1 (Click to Show/Hide)
function solve(input) {
  var orig = {};
  input.forEach(function(e) {
    orig[e.replace(/\s/g, "").toLowerCase()] = e;
  })

  input = Object.keys(orig)
  var map = {};
  for (var i = 0; i < input.length - 1; i++) {
    var pair0 = input[i];
    for (var j = i + 1; j < input.length; j++) {

      var pair = [pair0, input[j]];
      var s = pair0 + pair[1];
      var key = s.split("").sort();

      var val = map[key] ? map[key] : [];
      val.push(pair);
      map[key] = val;
    }
  }

  var result = [];
  Object.keys(map).forEach((key) => {

    for (var i = 0; i < map[key].length - 1; i++) {
      var a = map[key][i];
      for (var j = i + 1; j < map[key].length; j++) {
        var b = map[key][j];

        if ((new Set([a[0], b[0], a[1], b[1]])).size < 4)
          continue;
        var from = [orig[a[0]], orig[a[1]]].sort()
        var to = [orig[b[0]], orig[b[1]]].sort()
        result.push({
          from,
          to
        })
      }
    }
  });

  return result;
}