Learn how to restore IP address in one article!

Learn how to restore IP address in one article!

[[426350]]

Recover IP address

Given a string containing only numbers, unpack it and return all possible IP address formats.

A valid IP address consists of exactly four integers (each integer is between 0 and 255 and cannot contain leading 0), separated by '.'.

For example: "0.1.2.201" and "192.168.1.1" are valid IP addresses, but "0.011.255.245", "192.168.1.312", and "[email protected]" are invalid IP addresses.

Example 1:

  • Input: s = "25525511135"
  • Output: ["255.255.11.135","255.255.111.35"]

Example 2:

  • Input: s = "0000"
  • Output: ["0.0.0.0"]

Example 3:

  • Input: s = "1111"
  • Output: ["1.1.1.1"]

Example 4:

  • Input: s = "010010"
  • Output: ["0.10.0.10","0.100.1.0"]

Example 5:

  • Input: s = "101023"
  • Output: ["1.0.10.23","1.0.102.3","10.1.0.23","10.10.2.3","101.0.2.3"]

hint:

  • 0 <= s.length <= 3000
  • s consists only of digits

Ideas

Before doing this question, it is best to do the task of splitting 131 into a palindrome string first.

I believe that when you first see this question, you will be confused.

In fact, as long as we realize that this is a cutting problem, we can use the backtracking search method to search out all possibilities for the cutting problem, which is very similar to the 131. Splitting palindrome string we just did.

The cutting problem can be abstracted into a tree structure, as shown in the figure:

Recover IP address

Back to the trilogy

  • Recursive parameters

In 131. Splitting a palindrome, we mentioned that the cutting problem is similar to the combinatorial problem.

startIndex is definitely required because the segmentation cannot be repeated and records the starting position of the next layer of recursive segmentation.

For this question, we also need a variable pointNum to record the number of commas added.

So the code is as follows:

  1. vector<string> result; // record results
  2. // startIndex: the starting position of the search, pointNum: the number of commas to add
  3. void backtracking(string& s, int startIndex, int pointNum) {
  • Recursion termination condition

The termination condition is different from that of 131. Splitting the palindrome string. This question clearly requires that it will only be divided into 4 segments, so the cutting line cannot be used as the termination condition to cut to the end, but the number of segments divided can be used as the termination condition.

pointNum indicates the number of commas. A pointNum of 3 means that the string is divided into 4 segments.

Then verify whether the fourth paragraph is legal, and if so, add it to the result set.

The code is as follows:

  1. if (pointNum == 3) { // When the number of commas is 3, the separation ends
  2. // Determine whether the fourth substring is legal, if legal, put it into result
  3. if (isValid(s, startIndex, s. size () - 1)) {
  4. result.push_back(s);
  5. }
  6. return ;
  7. }
  • Single-layer search logic

In 131. Splitting a Palindrome String, we have already discussed how to extract substrings during loop traversal.

In the for (int i = startIndex; i < s.size(); i++) loop, the interval [startIndex, i] is the intercepted substring, and it is necessary to determine whether this substring is legal.

If it is legal, add a symbol after the string to indicate that it has been split.

If it is illegal, end the current loop, as shown in the figure where the branch is cut off:

Recover IP address

Then comes the process of recursion and backtracking:

When calling recursively, the startIndex of the next recursive layer should start from i+2 (because the separator needs to be added to the string), and the number of separators pointNum should be +1.

When backtracking, just delete the separator . that you just added, and pointNum should also be -1.

The code is as follows:

  1. for ( int i = startIndex; i < s. size (); i++) {
  2. if (isValid(s, startIndex, i)) { // Determine whether the substring in the interval [startIndex,i] is legal
  3. s.insert (s.begin () + i + 1 , '.' ) ; // Insert a comma after i
  4. pointNum++;
  5. backtracking(s, i + 2, pointNum); // After inserting the comma, the starting position of the next substring is i+2
  6. pointNum --; // backtrack  
  7. s.erase( s.begin () + i + 1); // Backtrack and delete the comma
  8. } else break; // Illegal, end the current loop directly
  9. }

Determine whether the substring is legal

The last step is to write a function to determine whether the rank is valid.

The following three points are mainly considered:

  • The number starting with 0 is illegal.
  • Non-positive integer characters are illegal in the segment
  • If the rank is greater than 255, it is illegal.

The code is as follows:

  1. // Determine whether the number composed of string s in the left-closed and closed interval [start, end ] is legal
  2. bool isValid(const string& s, int start, int   end ) {
  3. if (start > end ) {
  4. return   false ;
  5. }
  6. if (s[start] == ​​'0' && start != end ) { // Numbers starting with 0 are illegal
  7. return   false ;
  8. }
  9. int num = 0;
  10. for ( int i = start; i <= end ; i++) {
  11. if (s[i] > '9' || s[i] < '0' ) { // Non-numeric characters are illegal
  12. return   false ;
  13. }
  14. num = num * 10 + (s[i] - '0' );
  15. if (num > 255) { // If it is greater than 255, it is illegal
  16. return   false ;
  17. }
  18. }
  19. return   true ;
  20. }

C++ Code

According to the backtracking algorithm, you should know this! The backtracking algorithm template given is:

  1. void backtracking(parameters) {
  2. if (termination condition) {
  3. Store the results;
  4. return ;
  5. }
  6.  
  7. for (select: elements in the current layer set (the number of node children in the tree is the size of the set)) {
  8. Processing nodes;
  9. backtracking(path, selection list); // recursion
  10. Backtracking, undoing processing results
  11. }
  12. }

You can write the following backtracking algorithm C++ code:

  1. class Solution {
  2. private:
  3. vector<string> result; // record results
  4. // startIndex: the starting position of the search, pointNum: the number of commas to add
  5. void backtracking(string& s, int startIndex, int pointNum) {
  6. if (pointNum == 3) { // When the number of commas is 3, the separation ends
  7. // Determine whether the fourth substring is legal, if legal, put it into result
  8. if (isValid(s, startIndex, s. size () - 1)) {
  9. result.push_back(s);
  10. }
  11. return ;
  12. }
  13. for ( int i = startIndex; i < s. size (); i++) {
  14. if (isValid(s, startIndex, i)) { // Determine whether the substring in the interval [startIndex,i] is legal
  15. s.insert (s.begin () + i + 1 , '.' ) ; // Insert a comma after i
  16. pointNum++;
  17. backtracking(s, i + 2, pointNum); // After inserting the comma, the starting position of the next substring is i+2
  18. pointNum --; // backtrack  
  19. s.erase( s.begin () + i + 1); // Backtrack and delete the comma
  20. } else break; // Illegal, end the current loop directly
  21. }
  22. }
  23. // Determine whether the number composed of string s in the left-closed and closed interval [start, end ] is legal
  24. bool isValid(const string& s, int start, int   end ) {
  25. if (start > end ) {
  26. return   false ;
  27. }
  28. if (s[start] == ​​'0' && start != end ) { // Numbers starting with 0 are illegal
  29. return   false ;
  30. }
  31. int num = 0;
  32. for ( int i = start; i <= end ; i++) {
  33. if (s[i] > '9' || s[i] < '0' ) { // Non-numeric characters are illegal
  34. return   false ;
  35. }
  36. num = num * 10 + (s[i] - '0' );
  37. if (num > 255) { // If it is greater than 255, it is illegal
  38. return   false ;
  39. }
  40. }
  41. return   true ;
  42. }
  43. public :
  44. vector<string> restoreIpAddresses(string s) {
  45. result.clear();
  46. if ( s.size () > 12) return result; // It is considered pruning
  47. backtracking(s, 0, 0);
  48. return result;
  49. }
  50. };

Summarize

This question covers all the difficulties in splitting strings that I listed in 131. Splitting palindromes.

In addition, this question also requires you to manipulate the string to add commas as separators and verify the legitimacy of the interval.

It can be said to be an enhanced version of 131. Split palindrome.

In the tree structure diagram of this article, I have drawn out the detailed analysis ideas. I believe that everyone will have a clearer mind after reading it!

This article is reprinted from the WeChat public account "Code Random Thoughts", which can be followed through the following QR code. To reprint this article, please contact the Code Random Thoughts public account.

<<:  Virtual operators in the 5G era: new opportunities and challenges coexist

>>:  What will happen to 5G base stations if power is cut off? Can operators handle the performance?

Recommend

How to use 5G spectrum efficiently? Both licensing and sharing are effective

Telecoms.com regularly invites third-party expert...

How does machine learning help 5G networks?

Machine Learning Machine learning is a field of e...

5G scenarios and technologies bring new security threats

The virtualization characteristics of 5G networks...

How to implement RBAC with API Gateway and OPA

Currently, in order to ensure that the right peop...

What did Chinese operators show the world at the Winter Olympics?

This Winter Olympics is full of technological con...

Teach you how to easily obtain local area network devices

[[430847]] Preface With the rapid development of ...

6 trends that will boost the impact of IoT in 2018

In 2016-2017, the trend of IoT was widely accepte...

Deutsche Telekom warns: Banning Huawei will hinder Europe's 5G development

Europe will fall behind the United States and Chi...

Summary of common troubleshooting methods for network broadband

1. FTTH troubleshooting steps Step 1: Check the s...

Xentain: $1.25/month-1GB/15GB SSD/1Gbps unlimited traffic/Fremont data center

New merchant, mainly with the discount code, the ...