GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #1079 - Letter Tile Possibilities - LeetCode JavaScript Solutions

Back to all solutions

#1079 - Letter Tile Possibilities

Medium • View on LeetCode • View on GitHub
String Hash Table Counting Backtracking

Problem Description

You have n tiles, where each tile has one letter tiles[i] printed on it.

Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.

Solution

/**
 * @param {string} tiles
 * @return {number}
 */
var numTilePossibilities = function(tiles) {
  const map = {};
  tiles.split('').forEach(t => map[t] = (map[t] ?? 0) + 1);
  return backtrack();

  function backtrack() {
    let count = 0;
    for (const key of Object.keys(map)) {
      if (!map[key]) continue;
      count += 1;
      map[key] -= 1;
      count += backtrack();
      map[key] += 1;
    }
    return count;
  }
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.