Group error messages that differ only in numbers

MediumAbout 25 minProblem 2 of 3 in Hash mapsDraft

The problem

Error messages often differ only in the numbers inside them: timeout after 30 ms on shard 4 and timeout after 31 ms on shard 9 are the same problem.

Group the messages whose text is identical once every run of digits is treated as the same. Return the groups as lists of positions in messages. Order the positions inside a group from first to last, and the groups by the position of their first message.

  • 0 ≤ messages.length ≤ 200,000
  • each message is at most 200 characters

Examples

  1. groupErrors(["timeout after 30 ms", "disk full", "timeout after 4 ms", "disk full"])returns [[0, 2], [1, 3]]
  2. groupErrors(["shard 1 down", "shard 12 down", "shard down"])returns [[0, 1], [2]]A message with no number is a different shape.
  3. groupErrors([])returns []

Hints

Hint 1 of 2

Turn each message into a key that is the same for messages that should be grouped: replace every run of digits with one placeholder.

Hint 2 of 2

Keep a Map from key to the list of positions. A Map keeps keys in the order they were first added, which is the order the groups need.

Editorial

Opens once you solve the problem, or after three checked attempts.

Your solution

Tab indents. Press Esc, then Tab, to leave the editor. Ctrl+Enter runs the examples.

Esc
↑ ↓ to move↵ to open