Why does the map size change?

72 views Asked by At
class Solution {
 public:
  bool checkInclusion(string s1, string s2) {
    int m = s1.size();
    int n = s2.size();
    unordered_map<char, int> s1_freq;
    unordered_map<char, int> s2_freq;
    int match = 0;
    for (char s : s1) {
      s1_freq[s]++;
    }
    cout << s1_freq.size() << endl << endl;
    int left = 0;
    int right = 0;
    int window = 0;
    while (left <= right && right < n) {
      s2_freq[s2[right]]++;
      window++;
      if (s1_freq.find(s2[right]) != s1_freq.end() &&
          s2_freq[s2[right]] == s1_freq[s2[right]]) {
        match++;
      }
      if (window == m) {
        cout << match << " " << s1_freq.size() << endl;
        if (match == s1_freq.size())
          return true;
        if (s2_freq[s2[left]] == s1_freq[s2[left]])
          match--;
        s2_freq[s2[left]]--;
        left++;
        window--;
      }
      right++;
    }
    return false;
  }
};

this is the output

2

0 2

0 3

1 4

2 5

1 5

0 5

0 6

sorry if the code is hard to read . I was trying to use sliding window to solve a problem involving finding if a permutation of s1 exist in s2 . i was using two maps . s1_freq is the unordered_map which has the frequency count of s1.

the count of frequency is 2 after i inserted the elements .

here is the test case : s1 : "ab" and s2 : "eidbaooo"

but the size of the s1 map seem to change as evident by the output . i suspect on the first if condition inside while . coz the output was 4 5 5 6 6 6 or something similar when i didnt have the find check in it .

sorry if the question is badly phrased

0

There are 0 answers