Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Restore IP Addresses
MEDIUM
15 mins
18 upvotes
Strings
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Strings
-
-
Other topics
Problem solved
Badge
Skill meter
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Restore IP Addresses

Contributed by
Shivam Kumar Singh
Medium
yellow-spark
0/80
Avg time to solve 15 mins
Success Rate 85 %
Share
18 upvotes

Problem Statement

You are given a string 'S' containing only digits. Your task is to find all possible IP addresses that can be obtained from string 'S' in lexicographical order.

Note:
A valid IP address consists of exactly four integers, each integer is between 0 and 255 separated by single dots, and cannot have leading zeros except in the case of zero itself.
For example:
The following are valid IP addresses:
0.1.24.255
18.5.244.1

Following are invalid IP addresses:
0.01.24.255  (as  01  contains one leading zero).
18.312.244.1 (as 312 not lies between 0 and 255).
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2 
2122
23579
Sample Output 1 :
[“2.1.2.2”]
[“2.3.5.79”, “2.3.57.9”, “2.35.7.9”, “23.5.7.9”]
Explanation for sample Input 1:
For the first test case, there is only one possible IP address that is [2.1.2.2]

For the second test case, all possible IP addresses are shown below.
[2.3.5.79], [2.3.57.9], [2.35.7.9], [23.5.7.9]
Sample Input 2 :
2
123
02300
Sample Output 2 :
[]
[“0.2.30.0”, “0.23.0.0”]
Explanation for sample Input 2:
For the first test case, there is no possible IP address. Therefore then answer is []

For the second test case, there are only 2 possible IP addresses are shown below.
[0.2.30.0], [0.23.0.0]
Reset Code
Full screen
Auto
copy-code
Console