Over a million developers have joined DZone.

Java Code Challenge: Scrabble Sets

Get ready to test your Java skills; this week, we have a Scrabble-themed challenge that should be a good opportunity to flex your Java 8 muscles.

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

Java Code Challenge is a new regular segment taking the best challenge from Reddit's dailyprogrammer. Things are a little different here as we're focused on Java. A working solution is not enough; we're looking for the cleanest Java code with tests. 3rd party libraries are welcome but if you can do it without it will be easier for others to comprehend.

If you can fit your solution in the comments then go for it, but preferably put your answer in GitHub and link in the comments. Next week we'll be sharing the best solutions and sharing the best code practices we see!  This challenge should be a good opportunity to flex your Java 8 muscles.

Description

Scrabble is a popular word game where players remove tiles with letters on them from a bag and use them to create words on a board. The total number of tiles, as well as the frequency of each letter, does not change between games.

For this challenge, we will be using the tile set from the English edition, which has 100 tiles total. Here's a reference for the distribution and point value of each tile.

Each tile will be represented by the letter that appears on it, with the exception that blank tiles are represented by underscores _.

Input Description

The tiles already in play are inputted as an uppercase string. For example, if 14 tiles have been removed from the bag and are in play, you would be given an input like this:

AEERTYOXMCNB_S

Output Description

You should output the tiles that are left in the bag. The list should be in descending order of the quantity of each tile left in the bag, skipping over amounts that have no tiles.

In cases where more than one letter has the same quantity remaining, output those letters in alphabetical order, with blank tiles at the end.

10: E
9: I
8: A
7: O
5: N, R, T
4: D, L, U
3: G, S
2: F, H, P, V, W
1: B, C, J, K, M, Q, Y, Z, _
0: X

If more tiles have been removed from the bag than possible, such as 3 Qs, you should give a helpful error message instead of printing the list.

Invalid input. More Q's have been taken from the bag than possible.

Challenge Inputs

  1. PQAREIOURSTHGWIOAE_
  2. LQTOONOEFFJZT
  3. AXHDRUIOR_XHJZUQEE

Challenge Outputs

1.

10: E
7: A, I
6: N, O
5: T
4: D, L, R
3: S, U
2: B, C, F, G, M, V, Y
1: H, J, K, P, W, X, Z, _
0: Q

2.

11: E
9: A, I
6: R
5: N, O
4: D, S, T, U
3: G, L
2: B, C, H, M, P, V, W, Y, _
1: K, X
0: F, J, Q, Z

3.

Invalid input. More X's have been taken from the bag than possible.

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.

Topics:
clean code ,code challenge

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}