描述
判断短字符串S中的所有字符是否在长字符串T中全部出现。
请注意本题有多组样例输入。
数据范围:1\le len(S),len(T)\le200\1≤len(S),len(T)≤200
进阶:时间复杂度:O(n)\O(n) ,空间复杂度:O(n)\O(n)
输入描述:
输入两个字符串。第一个为短字符串,第二个为长字符串。两个字符串均由小写字母组成。
输出描述:
如果短字符串的所有字符均在长字符串中出现过,则输出字符串"true"。否则输出字符串"false"。
示例1
输入:
bc abc
复制输出:
true
复制说明:
其中abc含有bc,输出"true"
#include <iostream>
#include <string.h>
#include <set>
bool stringMatching(std::string shortStr, std::string longStr)
{
std::set<char> shortSet;
for(int i = 0; i < shortStr.length(); ++i)
{
shortSet.insert(shortStr[i]);
}
std::set<char> longSet;
for(int i = 0; i < longStr.length(); ++i)
{
longSet.insert(longStr[i]);
}
shortStr.clear();
std::set<char>::iterator itershort = shortSet.begin();
while(itershort != shortSet.end())
{
shortStr += *itershort;
itershort++;
}
longStr.clear();
std::set<char>::iterator iterlong = longSet.begin();
while(iterlong != longSet.end())
{
longStr += *iterlong;
iterlong++;
}
for(int i = 0; i < shortStr.length(); ++i)
{
int count = 0;
for(int j = 0; j < longStr.length(); ++j)
{
count++;
char shortc = shortStr[i];
char longc = longStr[j];
if(shortStr[i] == longStr[j])
{
break;
}
if(count == longStr.length() - 1)
{
if(shortStr[i] != longStr[longStr.length()-1])
{
return false;
}
}
}
}
return true;
}
int main()
{
// while(1)
// {
std::string shortStr;
getline(std::cin, shortStr);
std::string longStr;
getline(std::cin, longStr);
bool flag = stringMatching(shortStr, longStr);
if(flag == 1)
{
std::cout << "true" << std::endl;
}
else
{
std::cout << "false" << std::endl;
}
// }
return 0;
}