leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1813.cpp (652B)


      1 class Solution {
      2   public:
      3     bool areSentencesSimilar(const string &sentence1, const string &sentence2) const {
      4         stringstream ss1(sentence1), ss2(sentence2);
      5         vector<string> s1, s2;
      6         string word;
      7 
      8         while (getline(ss1, word, ' '))
      9             s1.push_back(word);
     10         while (getline(ss2, word, ' '))
     11             s2.push_back(word);
     12 
     13         if (size(s1) > size(s2)) swap(s1, s2);
     14 
     15         int i = 0, j = 0;
     16         const int n = size(s1), m = size(s2);
     17         while (i < n && s1[i] == s2[i])
     18             i++;
     19         while (j < n && s1[n - j - 1] == s2[m - j - 1])
     20             j++;
     21 
     22         return i + j >= n;
     23     }
     24 };