這題是我功力不足
對於處理string不夠外, 還有沒有看出他的解法
其實就是兩個陣列都給two point, 從頭尾開始match
一旦頭不一樣, 就比尾,
直到兩個都不一樣就是回傳false
為什麼呢?我們可以接受從缺,
但這個缺必須夾在頭尾之間, 舉一個例子
s1 = "of"
s2 = "a lot of x"
可以看到, 我沒辦法從s1往一個方下補足s2要的單詞所以對於s2來說
start = “a”, end = “x”, 兩個都不是s1的單詞, 故直接就回傳false
class Solution {
public:
bool areSentencesSimilar(string sentence1, string sentence2) {
vector<string> s1;
vector<string> s2;
string word;
stringstream iss1(sentence1);
while(iss1>>word){
s1.push_back(word);
}
stringstream iss2(sentence2);
while(iss2>>word){
s2.push_back(word);
}
int start1 = 0;
int start2 = 0;
int end1 = s1.size() - 1;
int end2 = s2.size() - 1;
while(start1<=end1 &&start2 <= end2){
if(s1[start1] == s2[start2]){
start1++;
start2++;
}else if(s1[end1] == s2[end2]){
end1--;
end2--;
}else{
return false;
}
}
return true;
}
};