Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

24/07/2023, 11:38 Check Permutation in Strings

**ChatGPT:**

can you explain the code with example of string s1: a b c and s2: b a x y z a b c code: /* Given 2 strings,
return true if s2 contains permutation of s1 Ex. s1 = "ab", s2 = "eidbaooo" -> true, s2 contains "ba" Sliding
window, expand + count down char, contract + count up char Time: O(n) Space: O(1) */ class Solution {
public: bool checkInclusion(string s1, string s2) { int m = s1.size(); int n = s2.size(); if (m > n) { return false;
} vector<int> count(26); for (int i = 0; i < m; i++) { count[s1[i] - 'a']++; count[s2[i] - 'a']--; } if
(isPermutation(count)) { return true; } for (int i = m; i < n; i++) { count[s2[i] - 'a']--; count[s2[i - m] - 'a']++;
if (isPermutation(count)) { return true; } } return false; } private: bool isPermutation(vector<int>& count) {
for (int i = 0; i < 26; i++) { if (count[i] != 0) { return false; } } return true; } };

------------------
**ChatGPT:**

Sure! The given code is a C++ implementation of a function checkInclusion that checks whether string
s2 contains any permutation of string s1. In other words, it determines if there exists a substring in s2 that
contains the same characters as s1, regardless of their order. The algorithm uses the sliding window
technique to efficiently search for the permutation.

Let's go through the code step by step and understand how it works with an example using s1 = "abc"
and s2 = "baxyzabc":

1. The function checkInclusion takes two string parameters, s1 and s2.

2. It initializes two variables m and n, which store the lengths of s1 and s2, respectively. In our example,
m = 3 and n = 8.

3. It performs a quick check to see if the length of s1 is greater than the length of s2. If it is, there's no
possibility of s2 containing a permutation of s1, so the function immediately returns false.

4. A vector count of size 26 (to represent the 26 lowercase English alphabet characters) is created to
store the count of each character in s1 and the corresponding characters in the current window of s2.

5. The loop runs for the first m characters of s1 and s2. For each character, it increments the count of that
character in s1 and decrements the count of the corresponding character in s2. This step essentially
sets up the initial window of length m in s2 for comparison with s1.

6. The function isPermutation is called to check if the count vector has all zero values, indicating
that the current window of s2 contains a permutation of s1. If it does, the function returns true, and
the answer is found.

7. If the initial window is not a permutation, the function proceeds with the sliding window approach.

8. The loop starts from index m and goes up to index n-1 in s2. In each iteration, it expands the window
by including the next character and excludes the first character of the window.

9. It decrements the count of the character going out of the window and increments the count of the new
character coming into the window.

10. Again, it calls the isPermutation function to check if the current window is a permutation of s1.
If it is, the function returns true, indicating that a permutation is found.

11. If no permutation is found in any of the windows, the function returns false.

Now, let's see how the algorithm works with the provided example:

https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 1/9
24/07/2023, 11:38 Check Permutation in Strings

s1 = "abc" and s2 = "baxyzabc"

1. Initialize m = 3 and n = 8.

2. Set up the count vector based on the first 3 characters of s1 and s2. The count vector would be
[1, 1, 1, 0, 0, 0, ..., 0].

3. Since the count vector has all zero values, we return true.

Thus, the output of this example would be true, indicating that s2 contains a permutation of s1. In this
case, the substring "abc" is a permutation of "abc".

------------------
**ChatGPT:**

can u visualise it

------------------
**ChatGPT:**

Certainly! Let's visualize the process step-by-step using the example s1 = "abc" and s2 =
"baxyzabc".

1. Initialize m = 3 and n = 8.

2. Set up the count vector based on the first 3 characters of s1 and s2. The count vector would be
[1, 1, 1, 0, 0, 0, ..., 0].

makefile Copy code


s1: a b c
s2: b a x
count: [1, 1, 1, 0, 0, ..., 0]

3. Since the count vector has all zero values, we return true. The substring "abc" is a permutation of
"abc".

Visualization of Sliding Window Technique:

less Copy code


Initial Window: [b a x]
count: [1, 1, 1, 0, 0, ..., 0]

Sliding Window: [a x y]
count: [0, 0, 1, -1, 0, ..., 0]

Sliding Window: [x y z]
count: [0, 0, 0, -1, -1, ..., 0]

Sliding Window: [y z a]
count: [-1, 0, 0, -1, -1, ..., 0]

Sliding Window: [z a b]
count: [-1, 0, 0, 0, -1, ..., 0]

Sliding Window: [a b c]

https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 2/9
24/07/2023, 11:38 Check Permutation in Strings

count: [0, 0, 0, 0, -1, ..., -1]

As we can see, when the sliding window is at index 3 (i.e., "z a b"), the count vector becomes [0, 0, 0,
0, -1, ..., -1]. This means that the substring "zab" is a permutation of "abc".

Thus, the function would return true as it finds that s2 contains a permutation of s1.

------------------
**ChatGPT:**

explain logic without code and visualise it

------------------
**ChatGPT:**

Certainly! Let's explain the logic of the algorithm without code and visualize it step-by-step using the
example s1 = "abc" and s2 = "baxyzabc".

Step 1: Initialization

We have two strings, s1 and s2.


We initialize two variables, m and n, which store the lengths of s1 and s2, respectively.
We create a vector count of size 26 to keep track of character counts.

Step 2: Count Character Occurrences

We run a loop to iterate over the first m characters of both s1 and s2.
For each character in s1, we increment its count in the count vector, and for each character in s2, we
decrement its count in the count vector.
This step sets up the initial window of length m in s2 for comparison with s1.

Step 3: Check Initial Window

We call a helper function isPermutation to check if the count vector has all zero values.
If the count vector has all zeros, it means the initial window of s2 is a permutation of s1. We return
true.

Step 4: Sliding Window

If the initial window is not a permutation, we proceed with the sliding window approach.
We start from index m and go up to index n-1 in s2.
In each iteration, we expand the window by including the next character and exclude the first character
of the window.
We decrement the count of the character going out of the window and increment the count of the new
character coming into the window.

Step 5: Check Permutations

After each window adjustment, we again call the isPermutation function to check if the current
window is a permutation of s1.
If the count vector has all zeros, it means the current window is a permutation of s1. We return
true.

Step 6: Return Result

https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 3/9
24/07/2023, 11:38 Check Permutation in Strings

If no permutation is found in any of the windows, we return false, indicating that s2 does not
contain any permutation of s1.

Now, let's visualize the process step-by-step using the example s1 = "abc" and s2 = "baxyzabc":

1. Initialize m = 3 and n = 8.

2. Set up the count vector based on the first 3 characters of s1 and s2.

makefile Copy code


s1: a b c
s2: b a x
count: [1, 1, 1, 0, 0, 0, ..., 0]

3. The initial window is not a permutation, so we proceed with the sliding window approach.

4. Slide the window one position to the right.

less Copy code


Sliding Window: [a x y]
count: [0, 0, 1, -1, 0, ..., 0]

5. Slide the window one position to the right.

less Copy code


Sliding Window: [x y z]
count: [0, 0, 0, -1, -1, ..., 0]

6. Slide the window one position to the right.

less Copy code


Sliding Window: [y z a]
count: [-1, 0, 0, -1, -1, ..., 0]

7. Slide the window one position to the right.

less Copy code


Sliding Window: [z a b]
count: [-1, 0, 0, 0, -1, ..., 0]

8. Slide the window one position to the right.

less Copy code


Sliding Window: [a b c]
count: [0, 0, 0, 0, -1, ..., -1]

9. The count vector has all zeros, indicating that the substring "zab" is a permutation of "abc".

10. We return true, indicating that s2 contains a permutation of s1.

That's how the algorithm works step-by-step to check if s2 contains any permutation of s1.

------------------
**ChatGPT:**

can u show the hash maps

https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 4/9
24/07/2023, 11:38 Check Permutation in Strings

------------------
**ChatGPT:**

Sure! To better understand the algorithm and visualize the hash maps used for counting characters in s1 and
the sliding window of s2, let's walk through the example s1 = "abc" and s2 = "baxyzabc" step-by-
step.

Step 1: Initialization

We initialize two hash maps:

1. count_s1: To store the count of characters in s1.


2. count_window: To keep track of the characters in the current window of s2.

Initially, both hash maps are empty.

Step 2: Count Character Occurrences

We run a loop to iterate over the first m characters of both s1 and s2.
For each character in s1, we increment its count in count_s1. For each character in s2, we
decrement its count in count_window.

css Copy code


s1: a b c
s2: b a x
count_s1: {'a': 1, 'b': 1, 'c': 1}
count_window: {'a': -1, 'b': 0, 'c': 0, 'x': 1}

Step 3: Check Initial Window

We check if count_s1 and count_window are equal. In this case, they are not equal, so we proceed to
the next step.

Step 4: Sliding Window

We start from index m and go up to index n-1 in s2.


In each iteration, we expand the window by including the next character and exclude the first character
of the window.
We decrement the count of the character going out of the window and increment the count of the new
character coming into the window.

Step 5: Check Permutations

After each window adjustment, we check if the current count_window is equal to count_s1.

css Copy code


Sliding Window: [a x y]
count_window: {'a': 0, 'b': 0, 'c': 1, 'x': 0, 'y': 0}

css Copy code


Sliding Window: [x y z]
count_window: {'a': 0, 'b': 0, 'c': 0, 'x': -1, 'y': 0, 'z': 1}

css Copy code


Sliding Window: [y z a]
count_window: {'a': -1, 'b': 0, 'c': 0, 'x': 0, 'y': -1, 'z': 0}
https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 5/9
24/07/2023, 11:38 Check Permutation in Strings

css Copy code


Sliding Window: [z a b]
count_window: {'a': 0, 'b': 0, 'c': 0, 'x': 0, 'y': 0, 'z': 0}

css Copy code


Sliding Window: [a b c]
count_window: {'a': 1, 'b': 1, 'c': 1, 'x': 0, 'y': 0, 'z': 0}

Step 6: Return Result

When the sliding window reaches index 3, the count_window becomes equal to count_s1, which
means the substring "zab" is a permutation of "abc". Therefore, the function returns true, indicating that s2
contains a permutation of s1.

I've provided the hash maps and their changes at each step of the algorithm. The algorithm efficiently uses
these hash maps to keep track of character counts and determine if a permutation is present in s2.

------------------
**ChatGPT:**

can u write the code using hash maps instead of array: class Solution { public: bool checkInclusion(string s1,
string s2) { int m = s1.size(); int n = s2.size(); if (m > n) { return false; } vector<int> count(26); for (int i = 0;
i < m; i++) { count[s1[i] - 'a']++; count[s2[i] - 'a']--; } if (isPermutation(count)) { return true; } for (int i = m;
i < n; i++) { count[s2[i] - 'a']--; count[s2[i - m] - 'a']++; if (isPermutation(count)) { return true; } } return
false; } private: bool isPermutation(vector<int>& count) { for (int i = 0; i < 26; i++) { if (count[i] != 0) {
return false; } } return true; } };

------------------
**ChatGPT:**

Sure! Let's modify the code to use hash maps instead of arrays. We'll use unordered_map from the C++
Standard Template Library (STL) to represent the character counts instead of the vector<int> count.

Here's the updated code:

cpp Copy code


#include <string>
#include <unordered_map>

class Solution {
public:
bool checkInclusion(string s1, string s2) {
int m = s1.size();
int n = s2.size();
if (m > n) {
return false;
}

unordered_map<char, int> count;

// Count the characters in s1


for (char ch : s1) {
count[ch]++;
}

https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 6/9
24/07/2023, 11:38 Check Permutation in Strings

// Count the characters in the initial window of s2


for (int i = 0; i < m; i++) {
count[s2[i]]--;
}

if (isPermutation(count)) {
return true;
}

// Sliding window approach


for (int i = m; i < n; i++) {
// Remove the character going out of the window
count[s2[i - m]]++;
// Add the new character coming into the window
count[s2[i]]--;
if (isPermutation(count)) {
return true;
}
}

return false;
}

private:
bool isPermutation(unordered_map<char, int>& count) {
for (const auto& entry : count) {
if (entry.second != 0) {
return false;
}
}
return true;
}
};

In this updated code, we've replaced the vector<int> count with an unordered_map<char,
int> count. The unordered_map is used to keep track of the character counts in s1 and the current
window of s2.

The rest of the logic remains the same, utilizing the sliding window technique to efficiently find permutations
of s1 in s2.

Note: Don't forget to include the necessary header files for the updated code to work correctly.

------------------
**ChatGPT:**

can u visualise it

------------------
**ChatGPT:**

Sure! Let's visualize the process step-by-step using the example s1 = "abc" and s2 = "baxyzabc".

Step 1: Initialization

https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 7/9
24/07/2023, 11:38 Check Permutation in Strings

We have two strings, s1 and s2.

Initialize m = 3 (length of s1) and n = 8 (length of s2).

Create an empty hash map count to store the character counts.

Step 2: Count Character Occurrences

We count the characters in s1 and store their occurrences in the count hash map.

yaml Copy code


s1: a b c
s2: b a x
count: {'a': 1, 'b': 1, 'c': 1}

Step 3: Check Initial Window

Check if the character counts of s1 and the initial window of s2 are equal. In this case, they are not equal, so
we proceed to the next step.

Step 4: Sliding Window

Start from index m and go up to index n-1 in s2.

In each iteration, we slide the window by including the next character and excluding the first character of the
window. We also update the count hash map accordingly.

css Copy code


Sliding Window: [a x y]
count: {'a': 0, 'b': 0, 'c': 1, 'x': 0, 'y': 0}

css Copy code


Sliding Window: [x y z]
count: {'a': 0, 'b': 0, 'c': 0, 'x': -1, 'y': 0, 'z': 1}

css Copy code


Sliding Window: [y z a]
count: {'a': -1, 'b': 0, 'c': 0, 'x': 0, 'y': -1, 'z': 0}

css Copy code


Sliding Window: [z a b]
count: {'a': 0, 'b': 0, 'c': 0, 'x': 0, 'y': 0, 'z': 0}

css Copy code


Sliding Window: [a b c]
count: {'a': 1, 'b': 1, 'c': 1, 'x': 0, 'y': 0, 'z': 0}

Step 5: Check Permutations

After each window adjustment, we check if the current count hash map is equal to the hash map
representing the counts of characters in s1.

In this example, when the sliding window reaches index 3, the count hash map becomes equal to {'a':
1, 'b': 1, 'c': 1} (same as s1).

Step 6: Return Result

Since the count hash map is equal to the hash map representing the counts of characters in s1, we return
true, indicating that s2 contains a permutation of s1.
https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 8/9
24/07/2023, 11:38 Check Permutation in Strings

The visualization shows how the sliding window technique and hash maps efficiently track the character
counts and find permutations of s1 in s2.

------------------

https://chat.openai.com/c/17317589-12eb-48b4-8311-b461e8821365 9/9

You might also like