-
-
Notifications
You must be signed in to change notification settings - Fork 50.2k
Restore IP Addresses #13367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restore IP Addresses #13367
Changes from all commits
0bb4a6d
f775794
3dcbd62
c6540aa
01dd97e
a8ad699
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """ | ||
| Restore IP Addresses | ||
| -> IP Address consists of exactly four integers seperated by single dots. | ||
| -> Each integer is between 0 and 255. | ||
| -> Exempli Gratia - 192.168.1.1 is valid ip address but 192.168@1.1 is an invalid ip address. | ||
|
|
||
| We are given with a string containing only digits , return all possible valid IP Addresses that | ||
| can be formed by inserting dots in the string. | ||
| --> Not allowed to reorder or remove any digits in the string. | ||
| --> Return valid IP addresses in any order. | ||
|
|
||
| Topics covered: Backtracking | ||
|
|
||
| Example: | ||
| Input: s = "25525511135" | ||
| Output: ["255.255.11.135","255.255.111.35"] | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| class Solution: | ||
| def restoreIpAddresses(self, s: str): | ||
| res = [] | ||
|
|
||
| def backtrack(start, path): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
| # Base case: if we have 4 parts and used all digits | ||
| if len(path) == 4: | ||
| if start == len(s): | ||
| res.append(".".join(path)) | ||
| return | ||
|
|
||
| # Try segments of length 1 to 3 | ||
| for l in range(1, 4): | ||
| if start + l > len(s): | ||
| break | ||
| segment = s[start : start + l] | ||
|
|
||
| # Skip invalid parts (leading zeros or > 255) | ||
| if (segment.startswith("0") and len(segment) > 1) or int(segment) > 255: | ||
| continue | ||
|
|
||
| backtrack(start + l, path + [segment]) | ||
|
|
||
| backtrack(0, []) | ||
| return res | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
backtracking/ip_address.py, please provide doctest for the functionrestoreIpAddressesPlease provide return type hint for the function:
restoreIpAddresses. If the function does not return a value, please provide the type hint as:def function() -> None:Variable and function names should follow the
snake_casenaming convention. Please update the following name accordingly:restoreIpAddressesPlease provide descriptive name for the parameter:
s