c++ solution


  • -1
    Y
    This post is deleted!

  • 0
    E

    @manjie.wang said in c++ solution:

    class Solution {
    public:
        string replaceWords(vector<string>& dict, string sentence) {
            int i,j;
            string res="";
            map<string, int> dm;
            vector<string> sv;
            for(i=0;i<dict.size();i++){
                dm[dict[i]] =1;
            }
            int start=0;
            for(i=1;i<sentence.length();i++){
                if(sentence[i]==' '){
                    string tmp = sentence.substr(start,i-start);
                    for(j=1;j<=tmp.size();j++){
                        if(dm.count(tmp.substr(0,j))){
                            tmp = tmp.substr(0,j);  
                            break;
                        }
                    }
                    res += tmp +" ";
                    start=i+1;
                }
            }
            string tmp = sentence.substr(start,i-start);
            for(j=1;j<=tmp.size();j++){
                if(dm.count(tmp.substr(0,j))){
                    tmp = tmp.substr(0,j);  
                    break;
                }
            }
            res += tmp;
    
            return res;
        }
    };
    

    Does this TLE or get accepted?


  • 0
    Y
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.