Advertisement
nikunjsoni

809

Jun 27th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int expressiveWords(string S, vector<string>& words) {
  4.         int res = 0;
  5.         for (auto &W : words) if (check(S, W)) res++;
  6.         return res;
  7.     }
  8.  
  9.     bool check(string S, string W) {
  10.         int n = S.size(), m = W.size(), j = 0;
  11.         for (int i = 0; i < n; i++)
  12.             if (j < m && S[i] == W[j]) j++;
  13.             else if (i > 1 && S[i - 2] == S[i - 1] && S[i - 1] == S[i]);
  14.             else if (0 < i && i < n - 1 && S[i - 1] == S[i] && S[i] == S[i + 1]);
  15.             else return false;
  16.         return j == m;
  17.     }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement