-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindLongestWord.cpp
39 lines (34 loc) · 967 Bytes
/
findLongestWord.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<string>
#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
bool isInclude(const string & first, const string& second){
int i = 0, j = 0;
while (i < first.size() && j < second.size())
{
if(first[i] == second[j]){
++i; ++j;
}else{
++i;
}
}
if(j == second.size() - 1)
return true;
return false;
}
string findLongestWord(string s, vector<string>& dictionary) {
string res;
for(int i = 0; i < dictionary.size(); ++i){
if(isInclude(s, dictionary[i])){
if(dictionary[i].size() > res.size()){
res = dictionary[i];
}else if(dictionary[i].size() == res.size() && dictionary[i] < res){
res = dictionary[i];
}
}
}
return res;
}
};