Java | Simple split and apply dots approach | Beats 80% | With explaination
Key Intuition: \n1. Split the string up in 2 parts in all possible ways. \n2. Now add dots at various possible places in each split part. Remember to skip insta
Key Intuition: \n1. Split the string up in 2 parts in all possible ways. \n2. Now add dots at various possible places in each split part. Remember to skip instances where pre dot part has leading zeros and post dot part has trailing zeros. Also do not forget to include the complete string itself without the dot in case it has no leading zeros. \n3. Find the cross product of list of strings obtained by putting the dot at various places in the left (xcord) and right (ycord) coordinates.\n4. Return this list of cross products of possible string combinations.\n```\n/*\nKey Intuition: \n1. Split the string up in 2 parts in all possible ways. \n2. Now add dots at various possible places in each split part. Remember to skip instances where pre dot part has leading zeros and post dot part has trailing zeros. Also do not forget to include the complete string itself without the dow in case it has no leading zeros. \n3. Find the cross product of list of strings obtained by putting the dot at various places in the left (xcord) and right (ycord) coordinates.\n4. Return this list of cross products of possible string combinations.\n\n*/\nclass Solution {\n    public List<String> ambiguousCoordinates(String s) {\n        List<String> result = new ArrayList<>();\n        \n        //Split the string up in 2 parts in all possible ways. \n        List<String[]> list = split(s.substring(1, s.length() - 1));\n\n        for(String[] split : list) {\n            \n            //Now add dots at various possible places in each split part. Remember to skip instances where pre dot part has leading zeros and post dot part has trailing zeros. Also do not forget to include the complete string itself without the dot in case it has no leading zeros.\n            List<String> xcords = dot(split[0]);\n            List<String> ycords = dot(split[1]);\n\t\t\t\n\t\t\t//Find the cross product of list of strings obtained by putting the dot at various places in the left (xcord) and right (ycord) coordinates.\n            for(String xcord : xcords) {\n                for(String ycord : ycords) {\n                    result.add("(" + xcord + ", " + ycord + ")");\n                }\n            }\n        }\n        return result;\n    }\n    \n    //Split the string up in 2 parts in all possible ways. \n    private List<String[]> split(String s) {\n        List<String[]> list = new ArrayList<>();\n        for(int i = 1; i < s.length(); i++) {\n            String[] splits = new String[2];\n            splits[0] = s.substring(0, i);\n            splits[1] = s.substring(i);\n            list.add(splits);\n        }\n        return list;\n    }\n    \n    private List<String> dot(String s) {\n        \n        List<String> list = new ArrayList<>();\n        \n        // do not forget to include the complete string itself without the dow in case it has no leading zeros.\n        if(!s.startsWith("0") || s.length() == 1) {\n            list.add(s);\n        }\n        \n        if(s.length() == 1) {\n            return list;\n        }\n        \n        //Now add dots at various possible places in each split part.\n        for(int i = 1; i < s.length(); i++) {\n            \n            //Remember to skip instances where pre dot part has leading zeros and post dot part has trailing zeros\n            String preDot = s.substring(0, i);\n            if(preDot.startsWith("0") && preDot.length() > 1) {\n                continue;\n            }\n            String postDot = s.substring(i);\n            if(postDot.endsWith("0")) {\n                continue;\n            }\n            list.add(preDot + "." + postDot);\n        }\n        return list;\n    }\n}\n```\n\n**Please upvote if this helped**
[]