Problem 555120 · medium · Phase 05 Advanced Algorithms & Graphs

Restore the Dots in a Four-Part Tag

backtracking · strings · validation

A warehouse labels shelves with tags of four numbers separated by dots, such as "10.2.3.0". Each number is between 0 and 255 and has no leading zero (so "0" is fine but "02" is not). A scanner dropped the dots and kept only the digits digits. Return every tag that the digits could have come from, keeping the digits in order. Tags may be returned in any order.

Examples

Input:  digits = "10230"
Output: ["10.2.3.0", "1.0.23.0", "1.0.2.30"]

Input:  digits = "0000"
Output: ["0.0.0.0"]

Input:  digits = "123"
Output: []
Explanation: three digits cannot form four numbers.

Constraints

  • 0 <= len(digits) <= 16, digits only

Goals

  • Split a string into a fixed number of parts with per-part rules
  • Prune using the number of characters left versus parts left
Starting Python…