
class Solution {
public:
bool checkInclusion(string s1, string s2)
{
int n = s1.length(), m = s2.length();
if (n > m) {
return false;
}
vector<int> cnt1(26), cnt2(26);
for (int i = 0; i < n; ++i) {
++cnt1[s1[i] - 'a'];
++cnt2[s2[i] - 'a'];
}
if (cnt1 == cnt2) {
return true;
}
for (int i = n; i < m; ++i)
{
cnt2[s2[i] - 'a']++;
cnt2[s2[i - n] - 'a']--;
if (cnt1 == cnt2)
{
return true;
}
}
return false;
}
};
此篇博客介绍了如何使用C++实现Solution类中的checkInclusion方法,用于判断两个字符串是否可以通过字符增删达到相同的字符计数。通过vector存储字符计数并进行比较,适用于字符串操作和动态规划问题。
5174

被折叠的 条评论
为什么被折叠?



