Restore IP Addresses LeetCode Programming Solutions | LeetCode Problem Solutions in C++, Java, & Python [💯Correct]

LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++

Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the Restore IP Addresses in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

About LeetCode

LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews. 

LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either EasyMedium, or Hard.

LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:

  • Mathematics/Basic Logical Based Questions
  • Arrays
  • Strings
  • Hash Table
  • Dynamic Programming
  • Stack & Queue
  • Trees & Graphs
  • Greedy Algorithms
  • Breadth-First Search
  • Depth-First Search
  • Sorting & Searching
  • BST (Binary Search Tree)
  • Database
  • Linked List
  • Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

Link for the ProblemRestore IP Addresses– LeetCode Problem

Restore IP Addresses– LeetCode Problem


valid IP address consists of exactly four integers separated by single dots. Each integer is between 0 and 255 (inclusive) and cannot have leading zeros.

  • For example, "" and "" are valid IP addresses, but """" and "[email protected]" are invalid IP addresses.

Given a string s containing only digits, return all possible valid IP addresses that can be formed by inserting dots into s. You are not allowed to reorder or remove any digits in s. You may return the valid IP addresses in any order.

Example 1:

Input: s = "25525511135"
Output: ["",""]

Example 2:

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

Example 3:

Input: s = "101023"
Output: ["","","","",""]


  • 0 <= s.length <= 20
  • s consists of digits only.
Restore IP Addresses– LeetCode Solutions
Restore IP Addresses Solution in C++:
class Solution {
  vector<string> restoreIpAddresses(const string& s) {
    vector<string> ans;

    dfs(s, 0, {}, ans);

    return ans;

  void dfs(const string& s, int start, vector<string>&& path,
           vector<string>& ans) {
    if (path.size() == 4 && start == s.length()) {
      ans.push_back(path[0] + "." + path[1] + "." + path[2] + "." + path[3]);
    if (path.size() == 4 || start == s.length())

    for (int length = 1; length <= 3; ++length) {
      if (start + length > s.length())
        return;  // out of bound
      if (length > 1 && s[start] == '0')
        return;  // leading '0'
      const string& num = s.substr(start, length);
      if (stoi(num) > 255)
      dfs(s, start + length, move(path), ans);
Restore IP Addresses Solution in Java:
class Solution {
  public List<String> restoreIpAddresses(final String s) {
    List<String> ans = new ArrayList<>();

    dfs(s, 0, new ArrayList<>(), ans);

    return ans;

  private void dfs(final String s, int start, List<String> path, List<String> ans) {
    if (path.size() == 4 && start == s.length()) {
      ans.add(String.join(".", path));
    if (path.size() == 4 || start == s.length())

    for (int length = 1; length <= 3; ++length) {
      if (start + length > s.length()) // out of bound
      if (length > 1 && s.charAt(start) == '0') // leading '0'
      final String num = s.substring(start, start + length);
      if (Integer.parseInt(num) > 255)
      dfs(s, start + length, path, ans);
      path.remove(path.size() - 1);
Restore IP Addresses Solution in Python:
class Solution:
  def restoreIpAddresses(self, s: str) -> List[str]:
    ans = []

    def dfs(start: int, path: List[int]) -> None:
      if len(path) == 4 and start == len(s):
        ans.append(path[0] + '.' + path[1] + '.' + path[2] + '.' + path[3])
      if len(path) == 4 or start == len(s):

      for length in range(1, 4):
        if start + length > len(s):
          return  # out of bound
        if length > 1 and s[start] == '0':
          return  # leading '0'
        num = s[start: start + length]
        if int(num) > 255:
        dfs(start + length, path + [num])

    dfs(0, [])

    return ans

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.