Skip to content

Commit c6540aa

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 3dcbd62 commit c6540aa

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

backtracking/ip_address.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
"""
2-
Restore IP Addresses
3-
-> IP Address consists of exactly four integers seperated by single dots.
4-
-> Each integer is between 0 and 255.
5-
-> Exempli Gratia - 192.168.1.1 is valid ip address but 192.168@1.1 is an invalid ip address.
6-
7-
We are given with a string containing only digits , return all possible valid IP Addresses that
8-
can be formed by inserting dots in the string.
9-
--> Not allowed to reorder or remove any digits in the string.
10-
--> Return valid IP addresses in any order.
11-
12-
Topics covered: Backtracking
13-
14-
Example:
15-
Input: s = "25525511135"
16-
Output: ["255.255.11.135","255.255.111.35"]
2+
Restore IP Addresses
3+
-> IP Address consists of exactly four integers seperated by single dots.
4+
-> Each integer is between 0 and 255.
5+
-> Exempli Gratia - 192.168.1.1 is valid ip address but 192.168@1.1 is an invalid ip address.
6+
7+
We are given with a string containing only digits , return all possible valid IP Addresses that
8+
can be formed by inserting dots in the string.
9+
--> Not allowed to reorder or remove any digits in the string.
10+
--> Return valid IP addresses in any order.
11+
12+
Topics covered: Backtracking
13+
14+
Example:
15+
Input: s = "25525511135"
16+
Output: ["255.255.11.135","255.255.111.35"]
1717
1818
"""
1919

@@ -33,10 +33,10 @@ def backtrack(start, path):
3333
for l in range(1, 4):
3434
if start + l > len(s):
3535
break
36-
segment = s[start:start + l]
36+
segment = s[start : start + l]
3737

3838
# Skip invalid parts (leading zeros or > 255)
39-
if (segment.startswith('0') and len(segment) > 1) or int(segment) > 255:
39+
if (segment.startswith("0") and len(segment) > 1) or int(segment) > 255:
4040
continue
4141

4242
backtrack(start + l, path + [segment])

0 commit comments

Comments
 (0)