Advertisement
Ashiqi_Shafi

Check one of permutation of string1 in string2

Feb 5th, 2023
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. class Solution {
  2. public:
  3.   bool checkInclusion(string s1, string s2) {
  4.     int n = size(s1), m = size(s2);
  5.     if (n > m) return false;
  6.     vector<int> hash1(26, 0), hash2(26, 0);
  7.     for (auto c : s1) hash1[c - 'a']++;
  8.     int i = 0, j = 0;
  9.     while (i < m) {
  10.       hash2[s2[i] - 'a']++;
  11.       if (i >= n) hash2[s2[j++] - 'a']--;
  12.       if (hash1 == hash2) return true;
  13.       i++;
  14.     }
  15.     return false;
  16.   }
  17. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement