top of page

Looking to master object-oriented and system design for tech interviews or career growth?

  • Improve your system design and machine coding skills.

  • Study with our helpful resources.

  • Prepare for technical interviews and advance your career.

**We're in beta mode and would love to hear your feedback.

Valid Ip Addresses InterviewBit Solution

May 21, 2021
1 min read

Problem Description:

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

A valid IP address must be in the form of A.B.C.D, where A, B, C and D are numbers from 0-255. The numbers cannot be 0 prefixed unless they are 0.

Example:

Given “25525511135”,

return [“255.255.11.135”, “255.255.111.35”]. (Make sure the returned strings are sorted in order)


Solution Approach:

Brute Force will work, you just have to insert 3 dots in between the string.


Time Complexity:

Since after each character we can either insert a dot or not. So we have two options for N characters, therefore time complexity is O(2^N).




A solution in C++:


1 Comment


JasmineQuinn
a day ago

https://buryjerry.com came to mind when you broke down the 2^N complexity, since that branching at every character really clarifies why brute force still works here. The backtracking approach with the 0-255 range check and no leading zeros feels like the part most people trip over in interviews. I've found that sketching the dot placement on paper first makes the recursion much easier to reason about than jumping straight into code.

Like
bottom of page