You are given a string made only of digits. You must insert exactly three dots into it, splitting it (without reordering or removing any digit) into four non-empty consecutive parts, in order. Each of the four parts must be a valid IPv4 octet: it must represent an integer between 0 and 255 inclusive, and it must not contain a leading zero unless the part is exactly the single character 0.
Count the number of distinct ways to place the three dots so that all four resulting parts are valid octets.
Line 1: a string s of digit characters 0-9.
A single integer: the number of valid ways to split s into four valid octets.
Example 1
Input
1111
Expected
1
Explanation
The string has length 4, so the only possible split gives four single-digit parts: 1, 1, 1, 1 — all valid octets — giving exactly 1 valid split.
Example 2
Input
123456
Expected
8
Explanation
Splitting "123456" into four ordered parts of length 1-3 that are each a valid octet (0-255, no disallowed leading zero) works in 8 different ways, such as 1.234.5.6 and 12.34.5.6.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →