Letter Tile Possibilities

MediumBacktrackingbacktracking

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You have a set of letter tiles described by the string tiles, where each character is one tile (the same letter can appear on several tiles). Return the number of distinct **non-empty** sequences of letters you can spell by laying down some of the tiles in a row, using each tile at most once. Two sequences are the same if they read the same, regardless of which physical tiles were used.

Examples

Input: tiles = "AAB"
Output: 8
The sequences are `A`, `B`, `AA`, `AB`, `BA`, `AAB`, `ABA`, `BAA`: 8 in total.
Input: tiles = "V"
Output: 1
Only the single-letter sequence `V` is possible.

Constraints

  • 1 <= tiles.length <= 7
  • `tiles` contains only uppercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More backtracking problems