帮忙看看为撒 leetcode OJ time out "Substring with Concatenation of All Words "# JobHunting - 待字闺中
c*z
1 楼
leetcode 这道题大家帮忙看看为撒 time out
class Solution {
public:
vector findSubstring(string S, vector &L) {
// check border case
vector ret;
if ( L.empty() || S.empty() )
return ret;
int i = 0;
int j = 0;
int num = L.size();
int length = L[0].size();
map mapSrc;
map mapDest;
for( int i = 0 ; i {
mapSrc[L[i]]++;
}
i=0;
while ( ( i + num * length - 1) < S.size())
{
j = i;
while( mapDest.size() < num )
{
string str = S.substr(j, length);
if ( mapSrc.find(str) == mapSrc.end() )
{
break;
}
else
{
auto iter = mapDest.find(str);
if ( iter == mapDest.end() || mapDest[str] < mapSrc[str]
)
{
mapDest[str]++;
}
else
{
break;
}
}
j = j + length;
}
if ( mapDest.size() == num )
{
ret.push_back(i);
}
mapDest.clear();
i = i +1;
}
return ret;
}
};
class Solution {
public:
vector
// check border case
vector
if ( L.empty() || S.empty() )
return ret;
int i = 0;
int j = 0;
int num = L.size();
int length = L[0].size();
map
map
for( int i = 0 ; i
mapSrc[L[i]]++;
}
i=0;
while ( ( i + num * length - 1) < S.size())
{
j = i;
while( mapDest.size() < num )
{
string str = S.substr(j, length);
if ( mapSrc.find(str) == mapSrc.end() )
{
break;
}
else
{
auto iter = mapDest.find(str);
if ( iter == mapDest.end() || mapDest[str] < mapSrc[str]
)
{
mapDest[str]++;
}
else
{
break;
}
}
j = j + length;
}
if ( mapDest.size() == num )
{
ret.push_back(i);
}
mapDest.clear();
i = i +1;
}
return ret;
}
};