Site Message
Only Premium Users can view the Question
Question 3
Overview
Solution
Scan the string from left to right.
Keep track of "lengths of chains"
e.g. aabbbbbcddd
- it has chain of a's of length 2
, chain of b's of length 5
, chain of c's of length 1
, chain of d's of length 3
Answer is product of length of all such chains 2 x 5 x 1 x 3 = 30
just keep performing modulus while doing the multiplications.
Time Complexity: O(|S|)
|S|
is size of the string.
Given two string S and T, determine if S can be shuffled in such a way that T does not occur as a subsequence of S.
bool solve(string s,string t)
{
map<char,int> sm,tm;
for(auto c:s)
sm[c]++;
for(auto c:t)
tm[c]++;
if((int)tm.size()==1)
{
for(auto x:tm)
{
if(sm[x.first]>=x.second)
return false;
}
}
return true;
}
Time Complexity: O(|S|+|T|)