Restore IP Addresses

MediumBacktrackingbacktracking

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A valid IPv4 address is four integers separated by dots, where each integer is between 0 and 255 and has no leading zeros ("0" is fine, "01" is not). Given a string s of digits, return every valid IP address that can be formed by inserting exactly three dots into s, without reordering, removing, or adding digits. The addresses may be returned in any order; return an empty list if none exist.

Examples

Input: s = "25525511135"
Output: ["255.255.11.135","255.255.111.35"]
Only these two splits give four valid parts.
Input: s = "0000"
Output: ["0.0.0.0"]
Each part must be a single 0.
Input: s = "101023"
Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]
Five valid splits exist.

Constraints

  • 1 <= s.length <= 20
  • `s` consists of digits only

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More backtracking problems