Valid Combinations of Numbers

Published: Jun 14, 2017

Various types of string related problems exist. Among them, splitting it to make something valid would be a typical one. For example, a string of numbers is given, “make valid IP addresses” is the example. Very similar problem is “make valid lottery numbers.”

When the given string is made by alphabetical characters, the problem may ask word breaks with a dictionary.

“Valid IP addresses” and “valid lottery numbers” are quite similar problems. I’m going to write a memo about these two here.

Problem Description - Restore IP Addresses

Given a string, restore valid IP addresses. For example, “25525511135” is given, the answer will be [“255.255.11.135”, “255.255.111.35”].

There are some constraints to make it the valid IP address.

  • valid IP addresses should have four parts separated by “.”(dot).
  • each digits must be between 0 to 255
  • if the character is ‘0’, it should not be followed by any number. ex) 255.0.0.1
  • must use all characters in the same order

When all of the constraints are met, the string becomes the valid IP address.

The idea to split a string to make IP addresses

If I think of the first part, it will be three patterns in maximum. For example, “25525511135” is given, “2”, “25”, and “255 are all valid numbers to start. When the first part is “2”, the valid second part will be “5” and “55.” Possible combinations create tree structure as in below.


            /           |          \
          /             |            \
         2             25            255
      /  |         /    |         /   |   \
    /    |      /       |       /     |      \
   5     55     5      52      2      25     255
  | \  / | \  / | \   /   \  / | \  /   \   / | \

When four parts of an IP address are created using valid numbers, another check runs: whether all given characters are used or not. If yes, I will add the IP address to the result list.

To solve this problem, I see a Depth First Search (DFS) works well. People have solved by various approaches, however, DFS is straightforward. This is because finding the answer is traversing a tree.

In each step, take one to three characters. When the number is valid, make it a part of IP address. Going deeper and take one to three characters, …(repeat)… In the end, check whether all characters are used to create valid four parts of IP address.

Java code for restoring valid IP addresses

Above prints:


[255.255.11.135, 255.255.111.35]
[0.0.0.0]

Time complexity: O(3^4).

Problem Description - Restore Lottery Numbers

Given a string, restore valid lottery numbers. For example, “4938532894754” is given, the answer will be [“49 38 53 28 9 47 54”].

The problem description sometimes starts from “uncle, Morty.”

Your favorite uncle, Morty, is crazy about the lottery and even crazier about how he picks his “lucky” numbers…

Like the IP address problem, there are some constraints to make it valid.

  • valid lottery numbers should have 7 parts separated by “ “(space).
  • each digits must be between 1 and 59
  • all digits are unique
  • must use all characters in the same order

The idea to split a string to make lottery numbers

This is almost identical to valid IP address problem. The small differences are from four parts to seven, from dot to space, and from 0-255 to 1-59. Relatively big difference is, in lottery problem, each digit is unique.

To check uniqueness, I added a set in the DFS interaction: add the number to set when going deeper, remove the number when coming back.

Except the differences above, the code is the same as valid IP addresses.

Java code for restoring valid lottery numbers

The result is:


[49 38 53 28 9 47 54]

[1 6 3 46 16 5 12, 1 6 3 46 16 51 2, 16 3 46 1 6 5 12, 16 3 46 1 6 51 2]

[]

Time complexity: O(2^7)

Resources

Latest Posts

Setting Up GCP Instance for Deep Learning

This post is going to be very different from what I write here. The content is a memo how I create a GCP (Google Cloud Platform) instance for Deep Learning. While I study algorithm stuff, I also have been studying Deep Learning. In my early days, I tried to train my Deep Learning model only on a laptop. My laptop is 2012 model MacBook Pro, so I would say it is reasonably fast. However, when it comes to Deep Learning, the training was quite painful on the such machine. Often, I ran the training over night to get a disappointed result. Still, I didn’t use any of pricey cloud environment since it was my personal study unrelated to my day job. I wanted to save money.

Complete Binary Tree

Problems which ask a binary tree traverse, add/delete nodes, etc. are popular in algorithm questions. The binary trees are often just a binary tree or binary search tree. Sometimes, the problem pinpoints a particular type of a binary tree, for example, a balanced binary tree or complete binary tree.

Catalan number

What is Catalan number ? The Catalan number belongs to the domain of combinatorial mathematics. It is a sequence of natural numbers such that: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, ... The sequence appears in counting problems. Wikipedia has the details about the sequence: Catalan Number.