bool isSubsetOf(const string& s, const string& t)
{
if(s.length() > t.length())
return false;
vector<int> table(256,0);
int cur = 0;
for(int i=0;i<s.length();++i)
{
if(table[s[i]] > 0) // if the char has been checked in t, decrease the counter by 1
{
--table[s[i]];
continue;
}
else if(cur == t.length())
return false;
while(cur < t.length())
{
if(t[cur] != s[i])
{
++table[t[cur]];
++cur;
}
else
{
++cur;
break;
}
}
}
return true;
}Return if string t contains all chars in s
最新推荐文章于 2026-06-26 13:05:21 发布
本文介绍了一种用于判断一个字符串是否为另一个字符串子集的高效算法,通过使用哈希表来跟踪字符出现次数,确保了算法的时间复杂度为O(n)。

269

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



