Skip to content
Decoder

Restore IP Addresses

Medium

Problem

A valid IPv4 address is exactly four numbers separated by dots, where each number is between 0 and 255 and has no leading zeros (0 is fine, 01 is not).

Given a string s of digits, insert three dots to form valid addresses. You may not reorder or drop digits. Return every valid address, in any order.

Example 1

Input
"25525511135"
Output
["255.255.11.135", "255.255.111.35"]

Example 2

Input
"0000"
Output
["0.0.0.0"]

Example 3

Input
"101023"
Output
["1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3", "101.0.2.3"]

Constraints

  • 1 <= s.length <= 20
  • s contains only digits.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.