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

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

node.js - Express and Redis - If session exists for this user, don't allow access -