Write timeout firstCode, Practice DFS
Class solution {public: vector <string> path; unordered_set <string> visited; vector <string> res; vector <string> findladders (string start, string end, unordered_set <string> & dict) {// start typing your C/C ++ Solution Below // do not write int main () function if (! Path. Empty () path. pop_back (); If (! Visited. Empty () visited. Clear (); While (! Res. empty () res. pop_back (); DFS (START, end, dict); int min = 0x7fffff; For (INT I = 0; I <res. size (); I ++) {If (RES [I]. size () <min) min = res [I]. size () ;}vector <vector <string> ans; For (INT I = 0; I <res. size (); I ++) {If (RES [I]. size () = min) ans. push_back (RES [I]);} return ans;} void DFS (string start, string end, unordered_set <string> & dict) {visited. insert (start); Path. push_back (start); int Len = start. length (); (INT I = 0; I <Len; I ++) for (Int J = 0; j <26; j ++) {string STR = start; STR [I] = 'A' + J; If (STR! = Start & dict. Find (STR )! = Dict. end () & visited. find (STR) = visited. end () {If (STR = END) {// note that return cannot be performed here, because the current node is not the last node (the second-to-last node ), return will go to the bottom-layer node path. push_back (STR); Res. push_back (PATH); Path. pop_back ();} else {DFS (STR, end, dict); Path. pop_back (); visited. erase (STR );}}}}};