Worst to best All solutions (368ms to 4 ms) C++
368 ms sol\n====================\n\tinline int max(int a,int b)\n\t{\n\t\treturn (a>b)?a:b;\n\t}\n\tclass Solution {\n\tpublic:\n\t\tunsigned int maxVal;\n\t\tb
368 ms sol\n====================\n\tinline int max(int a,int b)\n\t{\n\t\treturn (a>b)?a:b;\n\t}\n\tclass Solution {\n\tpublic:\n\t\tunsigned int maxVal;\n\t\tbool checkunique(string& str)\n\t\t{\n\t\t\tvector<int> vec(26, 0);\n\t\t\tfor (int i = 0; str[i] != \'\\0\'; i++)\n\t\t\t{\n\t\t\t\tif (vec[str[i] - \'a\'] == 1)\n\t\t\t\t\treturn false;\n\t\t\t\tvec[str[i] - \'a\'] = 1;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\n\t\tint maxLength(vector<string>& arr) {\n\t\t\tmaxVal = 0;\n\t\t\tint loop_count = pow(2, arr.size());\n\t\t\tstring str;\n\t\t\tint idx = 0,j,flag;\n\t\t\tbool vec[26];\n\t\t\tfor (int i = 1; i < loop_count; i++)\n\t\t\t{\n\t\t\t\tidx = 0;\n\t\t\t\tstr = "";\n\t\t\t\tj = i;\n\t\t\t\tflag = 0;\n\t\t\t\tfor (int k = 0; k < 26; k++)\n\t\t\t\t\tvec[k] = 0;\n\t\t\t\twhile (j)\n\t\t\t\t{\n\t\t\t\t\tif (j & 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tstr += arr[idx];\n\t\t\t\t\t\tfor (int k = 0; arr[idx][k]; k++)\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tif (vec[arr[idx][k] - \'a\'])\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tflag = 1;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tvec[arr[idx][k] - \'a\'] = 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (flag)\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tidx++;\n\t\t\t\t\tj >>= 1;\n\t\t\t\t}\n\t\t\t\tif (flag == 0)\n\t\t\t\t{\n\t\t\t\t\tmaxVal = max(maxVal, str.size());\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn maxVal;\n\t\t}\n\t};\n\n76 ms Sol\n============\n\tclass Solution {\n\tpublic:\n\t\tint maxVal;\n\t\tbool checkunique(string& str)\n\t\t{\n\t\t\tvector<int> vec(26, 0);\n\t\t\tfor (int i = 0; str[i] != \'\\0\'; i++)\n\t\t\t{\n\t\t\t\tif (vec[str[i] - \'a\'] == 1)\n\t\t\t\t\treturn false;\n\t\t\t\tvec[str[i] - \'a\'] = 1;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t\tvoid fun(vector<string>& arr, int idx, string str)\n\t\t{\n\t\t\tif (maxVal < str.size())\n\t\t\t\tmaxVal = str.size();\n\t\t\tif (idx == arr.size())\n\t\t\t\treturn;\n\n\t\t\tstring str_included = str + arr[idx];\n\t\t\tif (checkunique(str_included))\n\t\t\t{\n\t\t\t\tfun(arr, idx+1, str_included);\n\t\t\t}\n\t\t\t// not include\n\t\t\tfun(arr, idx + 1, str);\n\t\t}\n\t\tint maxLength(vector<string>& arr) {\n\t\t\tmaxVal = 0;\n\t\t\tint sz = arr.size();\n\t\t\tvector<string> new_arr;\n\t\t\tfor (int i = 0; i < sz; i++)\n\t\t\t\tif (checkunique(arr[i]))\n\t\t\t\t\tnew_arr.push_back(arr[i]);\n\t\t\tfun(new_arr, 0, "");\n\t\t\treturn maxVal;\n\t\t}\n\t};\n\t\n52 ms Sol\n=============\n\tclass Solution {\n\tpublic:\n\t\tint maxVal;\n\t\tbool checkunique(string & str)\n\t\t{\n\t\t\tvector<int> vec(26,0);\n\t\t\tfor(int i = 0 ; str[i]!= \'\\0\' ; i++)\n\t\t\t{\n\t\t\t\tif(vec[str[i]-\'a\'] ==1)\n\t\t\t\t\treturn false;\n\t\t\t\tvec[str[i]-\'a\'] =1;\n\t\t\t}\n\t\t\treturn true;\n\t\t}\n\t\tvoid fun(vector<string>&arr,int idx,string str)\n\t\t{\n\t\t\tif(!checkunique(str))\n\t\t\t\treturn;\n\n\t\t\tif(maxVal < str.size())\n\t\t\t\tmaxVal = str.size();\n\n\t\t\tfor(int i = idx+1 ; i < arr.size() ; i++)\n\t\t\t{\n\t\t\t\tfun(arr,i,str+arr[i]);\n\t\t\t}\n\t\t}\n\t\tint maxLength(vector<string>& arr) {\n\t\t\tmaxVal = 0;\n\t\t\t//string str = arr[0];\n\t\t\tfor(int i = 0 ; i < arr.size() ; i++)\n\t\t\t\tfun(arr,i,arr[i]);\n\t\t\treturn maxVal;\n\t\t}\n\t};\n28 ms Sol (whole iterative)\n================\n\tclass Solution {\n\tpublic:\n\t\tunsigned int maxVal;\n\t\tint maxLength(vector<string>& arr) {\n\t\t\tmaxVal = 0;\n\t\t\tint sz = arr.size();\n\t\t\tint loop_count = pow(2, arr.size());\n\t\t\tstring str;\n\t\t\tint idx = 0,j,flag;\n\n\t\t\tvector<int> encoded_arr(sz, 0);\n\t\t\tfor (int i = 0; i < sz; i++)\n\t\t\t{\n\t\t\t\tfor (int j = 0; arr[i][j]; j++)\n\t\t\t\t{\n\t\t\t\t\tint bit_val = (1 << (arr[i][j] - \'a\'));\n\t\t\t\t\tif ((encoded_arr[i] & bit_val) != 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tencoded_arr[i] = -1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tencoded_arr[i] |= bit_val;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfor (int i = 1; i < loop_count; i++)\n\t\t\t{\n\t\t\t\tidx = 0;\n\t\t\t\tint curr_sz = 0;\n\t\t\t\tj = i;\n\t\t\t\tflag = 0;\n\t\t\t\tint encoded_local = 0;\n\t\t\t\twhile (j)\n\t\t\t\t{\n\t\t\t\t\tif (j & 1)\n\t\t\t\t\t{\n\t\t\t\t\t\tcurr_sz += arr[idx].size();\n\t\t\t\t\t\tif (encoded_arr[idx] == -1 || encoded_local & encoded_arr[idx])\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tflag = 1;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tencoded_local |= encoded_arr[idx];\n\t\t\t\t\t}\n\t\t\t\t\tidx++;\n\t\t\t\t\tj >>= 1;\n\t\t\t\t}\n\t\t\t\tif (flag == 0 && maxVal < curr_sz)\n\t\t\t\t{\n\t\t\t\t\tmaxVal = curr_sz;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn maxVal;\n\t\t}\n\t};\n4 ms Sol\n==========\n\tclass Solution {\n\tpublic:\n\t\tint maxVal;\n\t\tvoid fun(vector<string>& arr, int idx, int curr_sz, vector<int>& encoded_arr, int encoded_val)\n\t\t{\n\t\t\tif (maxVal < curr_sz)\n\t\t\t\tmaxVal = curr_sz;\n\t\t\tif (idx == arr.size())\n\t\t\t\treturn;\n\n\t\t\tint new_sz = curr_sz + arr[idx].size();\n\t\t\tif (encoded_arr[idx] != -1 && (encoded_val & encoded_arr[idx]) == 0)\n\t\t\t{\n\t\t\t\tfun(arr, idx + 1, new_sz, encoded_arr, encoded_val | encoded_arr[idx]);\n\t\t\t}\n\t\t\t// not include\n\t\t\tfun(arr, idx + 1, curr_sz, encoded_arr, encoded_val);\n\t\t}\n\t\tint maxLength(vector<string>& arr) {\n\t\t\tmaxVal = 0;\n\t\t\tint sz = arr.size();\n\n\t\t\tvector<int> encoded_arr(sz, 0);\n\t\t\tfor (int i = 0; i < sz; i++)\n\t\t\t{\n\t\t\t\tfor (int j = 0; arr[i][j]; j++)\n\t\t\t\t{\n\t\t\t\t\tint bit_val = (1 << (arr[i][j] - \'a\'));\n\t\t\t\t\tif ((encoded_arr[i] & bit_val) != 0)\n\t\t\t\t\t{\n\t\t\t\t\t\tencoded_arr[i] = -1;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t\tencoded_arr[i] |= bit_val;\n\t\t\t\t}\n\t\t\t}\n\t\t\tfun(arr, 0, 0, encoded_arr, 0);\n\t\t\treturn maxVal;\n\t\t}\n\t};
['C++']