93. Restore IP Addresses

93 Restore IP Addresses

Total Accepted: 58138 Total Submissions: 243785 Difficulty: Medium

Given a string containing only digits, restore it by returning all possible valid IP address combinations.
For example:
Given "25525511135",
return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)

Hide Tags Backtracking String

public class Solution {
    public List<String> restoreIpAddresses(String s) {
        // use backtrack, divides the string s into 4 substring: s1, s2, s3, s4. 
        // for each part (xxx), we have three options (1, 2, 3)
        // https://leetcode.com/discuss/15098/very-simple-dfs-solution
        List<String> res = new ArrayList<>();
        backtrack(res, s, "", 0, 0);
        return res;
    }
    
    public void backtrack(List<String> res, String s, String ip, int pos, int count) {
        if (count > 4) return ;
        if (count == 4 && pos == s.length())  res.add(ip);
        
        for( int i = 1; i <= 3; i++) {
            if (pos + i > s.length()) return;
            
            String str = s.substring(pos, pos + i);
            
            if ((str.startsWith("0") && str.length() > 1)  || (Integer.parseInt(str) >= 256)) return;
            backtrack(res, s, ip + str + (count ==3 ? "" : "."), pos + i, count + 1);
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容