import java.util.*;


public class Solution {

    private List<List<String>> ipList = new ArrayList<>();

    /**
     *
     * @param s string字符串
     * @return string字符串ArrayList
     */
    public ArrayList<String> restoreIpAddresses (String s) {
        // write code here
        if (s == null || s.length() < 1) {
            return new ArrayList<>();
        }
        dfs(s, 0, new ArrayList<>());
        ArrayList<String> res = new ArrayList<>();
        for (List<String> list : ipList) {
            res.add(list2Ip(list));
        }
        return res;
    }

    private void dfs(String str, int start, List<String> list) {
        int n = str.length();
        if (start == n && list.size() == 4) {
            ipList.add(new ArrayList<>(list));
            return;
        }
        for (int i = start; i < n; i++) {
            if (i - start >= 1 && str.charAt(start) == '0') {
                break;
            }
            if (Integer.parseInt(str.substring(start, i + 1)) > 255) {
                break;
            }
            list.add(str.substring(start, i + 1));
            dfs(str, i + 1, list);
            list.remove(list.size() - 1);
        }
    }

    private String list2Ip(List<String> list) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 3; i++) {
            sb.append(list.get(i)).append(".");
        }
        sb.append(list.get(3));
        return sb.toString();
    }
}