s4553711
7/31/2017 - 3:09 PM

542.cpp

class Solution {
public:
    string findLongestWord(string s, vector<string>& d) {
        if (s == "" || d.size() == 0) return "";
        string result;
        int lens = s.length();
        for(int i = 0; i < d.size(); i++) {
            int len_str = d[i].length();
            int j = 0;
            for(int k = 0; k < lens && j < len_str; k++) {
                if (s[k] == d[i][j]) ++j;
            }
            if (j == len_str) {
                if (len_str > result.length()) {
                    result.replace(result.begin(), result.end(), d[i]);
                }
                if (len_str == result.length() && d[i] < result) {
                    result.replace(result.begin(), result.end(), d[i]);
                }
            }
        }
        return result;
    }
};