C++, easy to understand backtracking idea
see inline comments\n\nclass Solution {\npublic:\n    vector<string> ambiguousCoordinates(string S) {\n        vector<string> res;\n        S = S.substr(1, S.le
see inline comments\n```\nclass Solution {\npublic:\n    vector<string> ambiguousCoordinates(string S) {\n        vector<string> res;\n        S = S.substr(1, S.length() - 2);\n        dfs(res, S, string(1, S[0]), 1, FIRST);\n        return res;\n    }\n\nprivate:\n    enum STATE {\n        FIRST,\n        FIRST_PERIOD,\n        SECOND,\n        SECOND_PERIOD,\n    };\n\n    void dfs(vector<string>& res, const string& S, string cur, int index, STATE state) {\n        // cout<<cur<<" enter"<<endl;\n        if (index == S.length()) {\n            // cout<<cur<<" final"<<endl;\n            if (state == SECOND || (state == SECOND_PERIOD && cur.back() != \'0\')) {\n                res.push_back("(" + cur + ")");\n            }\n            return;\n        }\n        // avoid leading 0, e.g., "012", afer \'0\' has to put \'.\' or \', \'\n        if ((state != FIRST || index != 1 || S[index - 1] != \'0\') && (state != SECOND || cur[cur.length() - 3] != \',\') || S[index - 1] != \'0\') {\n            // cout<<cur<<" keep"<<endl;\n            dfs(res, S, cur + S[index], index + 1, state);\n        }\n        // \'.\' can be added at any time if not added yet (will check ending \'0\' of a decimal in two cases: 1) when add a comma, 2) at the end when push into res)\n        if (state == FIRST || state == SECOND) {\n            // cout<<cur<<" add dot"<<endl;\n            dfs(res, S, cur + "." + S[index], index + 1, state == FIRST? FIRST_PERIOD : SECOND_PERIOD);\n        }\n        // \',\' can be added when if decimal not ended with \'0\'\n        if (state == FIRST || (state == FIRST_PERIOD && S[index - 1] != \'0\')) {\n            // cout<<cur<<" add comma"<<endl;\n            dfs(res, S, cur + ", " + S[index], index + 1, SECOND);\n        }\n    }\n};\n```
[]