Leetcode 567. Permutation in String
Leetcode Problem 567: Permutation in String
Given two strings s1
and s2
, return true
if s2
contains a permutation of s1
, or false
otherwise.
In other words, return true
if one of s1
’s permutations is the substring of s2
.
Example 1:
Input: s1 = "ab", s2 = "eidbaooo"
Output: true
Explanation: s2 contains one permutation of s1 ("ba").
Example 2:
Input: s1 = "ab", s2 = "eidboaoo"
Output: false
s1
or s2
is an empty string?
s1
is empty, we can consider that an empty string is a permutation of any string, and we should return true
. If s2
is empty, then no permutation of s1
can be found, thus we should return false
.We’ll use the sliding window approach combined with a character frequency count to solve this problem efficiently.
s1
.s1
on s2
.s1
.s2
contains a permutation of s1
.#include <iostream>
#include <vector>
using namespace std;
bool matches(const vector<int>& s1_map, const vector<int>& s2_map) {
for (int i = 0; i < 26; ++i) {
if (s1_map[i] != s2_map[i]) {
return false;
}
}
return true;
}
bool checkInclusion(string s1, string s2) {
if (s1.length() > s2.length()) return false;
vector<int> s1_map(26, 0);
vector<int> s2_map(26, 0);
for (char c : s1) {
s1_map[c - 'a']++;
}
int window_size = s1.length();
for (int i = 0; i < s2.length(); ++i) {
s2_map[s2[i] - 'a']++;
if (i >= window_size) {
s2_map[s2[i - window_size] - 'a']--;
}
if (i >= window_size - 1) {
if (matches(s1_map, s2_map)) {
return true;
}
}
}
return false;
}
int main() {
// Test cases
cout << checkInclusion("ab", "eidbaooo") << endl; // Output: true
cout << checkInclusion("ab", "eidboaoo") << endl; // Output: false
return 0;
}
n1
is the length of s1
.n2
is the length of s2
.This is efficient for the given problem constraints and ensures that we check all potential substrings in s2
while maintaining an optimal runtime.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?