leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1813.cpp (652B)
0 class Solution { 1 public: 2 bool areSentencesSimilar(const string &sentence1, const string &sentence2) const { 3 stringstream ss1(sentence1), ss2(sentence2); 4 vector<string> s1, s2; 5 string word; 6 7 while (getline(ss1, word, ' ')) 8 s1.push_back(word); 9 while (getline(ss2, word, ' ')) 10 s2.push_back(word); 11 12 if (size(s1) > size(s2)) swap(s1, s2); 13 14 int i = 0, j = 0; 15 const int n = size(s1), m = size(s2); 16 while (i < n && s1[i] == s2[i]) 17 i++; 18 while (j < n && s1[n - j - 1] == s2[m - j - 1]) 19 j++; 20 21 return i + j >= n; 22 } 23 };