c++ - Determine running time of the code -


i have written following dp code today, worked fine got points in submission (here problem). not able determine running time of code. feel o(n^2 * log d) can't prove it.

class solution { public:     unordered_map<string, bool> m;     bool wordbreak(string s, unordered_set<string>& worddict) {         int n = s.length();         string t = "";         for(int i=0;i<n;i++){             t += s.at(i);             //cout << t << endl;             if(worddict.find(t) != worddict.end()){                 m[t] = true;                 string x = "";                 for(int j=i+1;j<n;j++){                     x += s.at(j);                 }                 if(x == ""){                     return true;                 }                 if(m.find(x) != m.end()){                     if(m[x] == true){                         return true;                     }else{                         continue;                     }                 }else{                     if(wordbreak(x, worddict)){                         m[x] = true;                         return true;                     }else{                         //cout << x << endl;                         m[x] = false;                         continue;                     }                 }             }else{                 //m[t] = false;             }         }         return false;     } }; 

it seems have o(n*n) complexity. use memorisation , every step of alrotithm produce @ least 1 new value in m. there n*n/2 substrings in string, find solution entire string n*n/2 passages in worst case.

ps: consider unordered_map works o(1).

edit:

it might more consider unordered_map works o(n) in case. m.find need calculate hash it's argument, wich string. may work faster if store indexes instead of string itself.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -